函数检查C链表中是否存在字符串
我正在为课堂编写一个程序,我的老师让我们在添加之前检查一下列表中是否已经有名字。我为此编写的代码似乎工作不正常函数检查C链表中是否存在字符串,c,function,linked-list,strcmp,C,Function,Linked List,Strcmp,我正在为课堂编写一个程序,我的老师让我们在添加之前检查一下列表中是否已经有名字。我为此编写的代码似乎工作不正常 void doAdd (waitListPtr hd) { /* get group size from input */ int size = getPosInt(); if (size < 1) { printf ("Error: Add command requires an integer value of at least 1\n"); printf ("Ad
void doAdd (waitListPtr hd)
{
/* get group size from input */
int size = getPosInt();
if (size < 1)
{
printf ("Error: Add command requires an integer value of at least 1\n");
printf ("Add command is of form: a <size> <name>\n");
printf (" where: <size> is the size of the group making the reservation\n");
printf (" <name> is the name of the group making the reservation\n");
return;
}
/* get group name from input */
char *name = getName();
if (NULL == name)
{
printf ("Error: Add command requires a name to be given\n");
printf ("Add command is of form: a <size> <name>\n");
printf (" where: <size> is the size of the group making the reservation\n");
printf (" <name> is the name of the group making the reservation\n");
return;
}
if(doesNameExist(hd, name) == TRUE){
printf("\nERROR: Name already on list");
}else{
printf ("Adding group \"%s\" of size %d\n", name, size);
addToList(&hd, size, name);
hd->groupStatus = PRESENT;
}
}
}
下面是我的链表结构的声明
typedef struct waitListStruct
{
char groupName[30];
int groupSize;
int groupStatus;
struct waitListStruct* next;
} waitList;
typedef waitList* waitListPtr;
下面是向列表中添加新节点的函数
void addToList(waitListPtr* hd, int size, char* name){
waitListPtr ptr = (waitListPtr) malloc (sizeof(waitList));
strcpy(ptr->groupName, name);
ptr->groupSize = size;
ptr->next = *hd;
*hd = ptr;
}
这是main中调用函数的部分
int main (int argc, char **argv)
{
waitListPtr head = NULL;
while ((ch = getNextNWSChar ()) != EOF)
{
if('a' == ch)
{
doAdd(head);
}
}
}
谁能告诉我我做错了什么
if(strcmp(ptr->groupName, &name)== 0){
这是不对的。我很惊讶你的编译器没有将其标记为错误
它需要:
if(strcmp(ptr->groupName, name)== 0){
// ^^ Drop the &
改进建议
该函数中的while
循环可以简化为:
while(ptr != NULL)
{
if (strcmp(ptr->groupName, name) == 0)
return TRUE;
ptr=ptr->next;
}
改变加法的逻辑
void addToList(waitListPtr* hd, int size, char* name){
waitListPtr ptr = (waitListPtr) malloc (sizeof(waitList));
strcpy(ptr->groupName, name);
ptr->groupSize = size;
if (*hd == NULL)
{
ptr->next = NULL;
} else {
ptr->next = *hd;
}
*hd = ptr;
}
希望解决问题#和平谢谢。刚试着去掉&,但我得到了同样的结果。知道还有什么问题吗?@wuddupjoe,这表示代码中的其他地方有问题。@wuddupjoe可能与您的
addToList
函数有关。问题是否可能是因为我如何将名称存储到链接列表中?这是我添加新节点的代码。void addToList(waitlistpr*hd,int size,char*name){waitlistpr=(waitlistpr)malloc(sizeof(waitList));strcpy(ptr->groupName,name);ptr->groupSize=size;ptr->next=*hd;*hd=ptr;}@wuddupjoe,我的建议是在调用strcmp
时应用我建议的修复程序来开始一篇新文章。如果我尝试用ptr.groupName替换ptr->groupName,我会收到一个错误,声明“在非结构或联合的内容中请求成员'groupName'”。我建议在这里发布完整的源代码,我可以帮助您。奇怪的是,hd不是ptr,你也不会出错。我目前无法发表新的帖子,因为它说我只能每90分钟发布一次问题。我有没有办法把代码传给你?@Sohil-Omer-typedef-waitList*waitListPtr;现在的问题是将名称添加到链接列表中
while(ptr != NULL){
// Remove & from the name , Check man page of strcmp
if(strcmp(ptr->groupName, name)== 0){
return TRUE;
// Remove this line as it is unreachable as you are returning
break;
}
void addToList(waitListPtr* hd, int size, char* name){
waitListPtr ptr = (waitListPtr) malloc (sizeof(waitList));
strcpy(ptr->groupName, name);
ptr->groupSize = size;
if (*hd == NULL)
{
ptr->next = NULL;
} else {
ptr->next = *hd;
}
*hd = ptr;
}