我正在通过使用(单个)链表编写一个简单的 C 员工数据库程序。目前我正在尝试编写下面给出的“删除员工”函数。
我正在尝试使用 while 循环遍历链表,并在当前指针名称字段与用户希望删除的名称匹配时停止。 (存储在 linestore 中。)
出于某种原因,无论如何,它都会一直循环遍历数据库直到结束。我尝试在每个阶段打印 linestore & currptr->name 的内容,它们看起来是正确的,所以我不知道我做错了什么。
如有任何帮助,我们将不胜感激。
删除员工函数:
char *lineptr;
char linestore[300];
lineptr = &linestore;
struct Employee *currptr = root;
struct Employee *prevptr = NULL;
fprintf(stderr, "\nPlease enter the EXACT name of the employee to be deleted.\n");
read_line(stdin, lineptr, MAX_NAME_LENGTH); //linestore function is working (checked)
while ( (currptr->name != linestore) & (currptr != NULL) )
{
fprintf(stderr, "\n***Searching database...***\n");
fprintf(stderr, "***The current record is %s", currptr->name);
prevptr = currptr;
currptr = currptr->next;
}
if ( currptr->name == linestore )
{
fprintf(stderr, "\n***Record DELETED.***\n");
}
最佳答案
currptr->name == linestore
==
不比较值。相反,它比较指针。对于字符串比较,您需要使用 strncmp
函数。
关于C 指针和链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18133340/