此代码用于在链接列表的开头添加员工配置文件。出于某些原因,我无法识别,这段代码只运行一次,然后它只打印了printf然后退出循环。即使在输入了大量记录之后,它仍然在做同样的事情。那么任何人都可以发现问题吗?
void insert_tobeg() {
char name[15];
struct employee *newPtr;
printf("\n\n\tEnter the record to be entered:");
gets(name);
if(strcmp(start->name, name) == 0) {
curr = (struct employee*)malloc(sizeof(struct employee));
employee_entry();
curr->newPtr = start;
start = curr;
printf("\n\n\tRecord has been added at the beggining!");
return;
}
}
答案 0 :(得分:0)
你是说while while而不是if,并删除return语句。
答案 1 :(得分:0)
您管理不正确,在第一个元素中覆盖名称,您需要写入当前分配的元素。 试试这样的事情
curr = (struct employee*)malloc( sizeof(struct employee));
employee_entry();
if( strcmp( curr->name, name) == 0)
{
curr->newPtr=start;
start = curr;
printf("\n\n\tRecord has been added at the beggining!");
return;
}
else
{
free(curr);
}