&引用;搜索“;c语言中的链表
我为我的链表菜单添加了一个“搜索”功能,我不知道我的代码中有什么错误。当我输入一个不在列表中的搜索键,而不是打印“搜索键:%s not Found!”,程序就会停止。如何修复它?关于如何改进我的课程有什么建议吗&引用;搜索“;c语言中的链表,c,search,linked-list,dynamic-allocation,C,Search,Linked List,Dynamic Allocation,我为我的链表菜单添加了一个“搜索”功能,我不知道我的代码中有什么错误。当我输入一个不在列表中的搜索键,而不是打印“搜索键:%s not Found!”,程序就会停止。如何修复它?关于如何改进我的课程有什么建议吗 #include <stdio.h> #include <conio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> struct lis
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
struct list
{
char name[20];
int age;
char gender[10];
struct list *next;
};
void main(void)
{
struct list *HEAD = NULL;
struct list *temp,*current, *trav;
struct list *prev,*temp1,*temp2;
char choice;
while(1)
{
clrscr();
printf("MENU\n");
printf("A) ADD\n");
printf("B) DISPLAY\n");
printf("C) DELETE\n");
printf("D) SEARCH\n");
printf("X) EXIT\n");
scanf("%c", &choice);
switch(toupper(choice))
{
case 'A':
temp= (struct list*)malloc(sizeof(struct list));
temp->next=NULL;
printf("Fill-Up the following:\n");
printf("Name:");
fflush(stdin);
gets(temp->name);
printf("Age:");
fflush(stdin);
scanf("%d",&temp->age);
printf("Gender:");
fflush(stdin);
gets(temp->gender);
if(HEAD == NULL)
{
HEAD = temp;
}
else if(HEAD!=NULL)
{
for(trav=HEAD; trav->next != NULL; trav= trav->next);
trav->next=temp;
}
else
{
printf("Not Enough Memory!\n");
}
break;
case 'B':
if(HEAD==NULL)
{
printf("Linked List is Empty!\n");
getch();
}
else{
for(trav=HEAD; trav != NULL; trav=trav->next )
{
printf("\nName: %s\n", trav->name);
printf("Age: %d\n", trav->age);
printf("Gender: %s\n\n", trav->gender);
}
getch();
}
break;
case 'C' :
temp1=( struct list*)malloc(sizeof(struct list));
temp1->next=NULL;
if(HEAD==NULL)
{
printf("No item to be delete. List is Empty!\n");
getch();
}
else {
printf("Enter The Name of the item you want to Delete: ");
fflush(stdin);
gets(temp1->name);
current=HEAD;
if(strcmp(temp1->name,current->name)== 0)
{
HEAD=HEAD->next;
free(current);
printf("Item has been successfully deleted from the list.\n");
getch();
}
else
{
for(prev=HEAD,trav=HEAD->next; strcmp(trav->name,temp1->name) == 1 ; trav=trav->next,prev=prev->next);
if(trav==NULL)
{
printf("Name: %s not found!", temp1->name);
getch();
}
else{
prev->next=trav->next;
free(trav);
printf("Item has been successfully deleted from the list.\n");
getch();
}
}
}
break;
case 'D':
temp2=( struct list*)malloc(sizeof(struct list));
temp2->next=NULL;
if(HEAD==NULL)
{
printf("No item to search. List is Empty.\n");
getch();
}
else{
printf("Enter Name (Search Key): ");
fflush(stdin);
gets(temp2->name);
int count=0;
struct list *trav2=HEAD;
while( trav2 !=NULL)
{
for(struct list *trav1=trav2; strcmp(trav1->name,temp2->name)!=0;trav1=trav1->next);
if(trav1!=NULL)
{
printf("\nName: %s\n", trav1->name);
printf("Age: %d\n", trav1->age);
printf("Gender: %s\n",trav1->gender);
trav2=trav1->next;
count++;
}
else {
trav2=NULL;
}
}
getch();
if(count==0)
{
printf("Search Key: %s Not Found!\n", temp2->name);
getch();
}
}
break;
case 'X':
if(HEAD!=NULL){free(HEAD); }
if(trav!=NULL){ free(trav); }
if(trav1!=NULL){ free(trav1); }
if(trav2!=NULL){ free(trav2); }
if(temp!=NULL){ free(temp); }
if(temp1!=NULL){ free(temp1); }
exit(1);
break;
}
}
}
#包括
#包括
#包括
#包括
#包括
结构列表
{
字符名[20];
智力年龄;
性别[10];
结构列表*下一步;
};
真空总管(真空)
{
结构列表*HEAD=NULL;
结构列表*temp、*current、*trav;
结构列表*prev、*temp1、*temp2;
字符选择;
而(1)
{
clrsc();
printf(“菜单\n”);
printf(“A)添加\n”);
printf(“B)显示\n”);
printf(“C)删除\n”);
printf(“D)搜索\n”);
printf(“X)退出\n”);
scanf(“%c”,选择(&c));
开关(toupper(选择))
{
案例“A”:
temp=(结构列表*)malloc(sizeof(结构列表));
temp->next=NULL;
printf(“填写以下内容:\n”);
printf(“名称:”);
fflush(stdin);
获取(临时->名称);
printf(“年龄:”);
fflush(stdin);
scanf(“%d”,&temp->age);
printf(“性别:”);
fflush(stdin);
获取(临时->性别);
if(HEAD==NULL)
{
压头=温度;
}
else if(HEAD!=NULL)
{
for(trav=HEAD;trav->next!=NULL;trav=trav->next);
trav->next=温度;
}
其他的
{
printf(“内存不足!\n”);
}
打破
案例“B”:
if(HEAD==NULL)
{
printf(“链接列表为空!\n”);
getch();
}
否则{
for(trav=HEAD;trav!=NULL;trav=trav->next)
{
printf(“\n名称:%s\n”,trav->name);
printf(“年龄:%d\n”,trav->Age);
printf(“性别:%s\n\n”,trav->Gender);
}
getch();
}
打破
案例“C”:
temp1=(结构列表*)malloc(sizeof(结构列表));
temp1->next=NULL;
if(HEAD==NULL)
{
printf(“没有要删除的项目。列表为空!\n”);
getch();
}
否则{
printf(“输入要删除的项的名称:”);
fflush(stdin);
获取(temp1->name);
电流=水头;
if(strcmp(temp1->name,current->name)==0)
{
头部=头部->下一步;
自由(电流);
printf(“项目已成功从列表中删除。\n”);
getch();
}
其他的
{
for(prev=HEAD,trav=HEAD->next;strcmp(trav->name,temp1->name)==1;trav=trav->next,prev=prev->next);
如果(trav==NULL)
{
printf(“名称:%s未找到!”,temp1->Name);
getch();
}
否则{
上一个->下一个=列车->下一个;
免费(trav);
printf(“项目已成功从列表中删除。\n”);
getch();
}
}
}
打破
案例“D”:
temp2=(结构列表*)malloc(sizeof(结构列表));
temp2->next=NULL;
if(HEAD==NULL)
{
printf(“没有要搜索的项目。列表为空。\n”);
getch();
}
否则{
printf(“输入名称(搜索键):”;
fflush(stdin);
获取(temp2->name);
整数计数=0;
结构列表*trav2=头部;
while(trav2!=NULL)
{
for(struct list*trav1=trav2;strcmp(trav1->name,temp2->name)!=0;trav1=trav1->next);
如果(trav1!=NULL)
{
printf(“\n名称:%s\n”,trav1->name);
printf(“年龄:%d\n”,trav1->Age);
printf(“性别:%s\n”,trav1->Gender);
trav2=trav1->next;
计数++;
}
否则{
trav2=NULL;
}
}
getch();
如果(计数=0)
{
printf(“未找到搜索键:%s!\n”,temp2->name);
getch();
}
}
打破
案例“X”:
如果(HEAD!=NULL){free(HEAD);}
如果(trav!=NULL){free(trav);}
如果(trav1!=NULL){free(trav1);}
如果(trav2!=NULL){free(trav2);}
如果(temp!=NULL){free(temp);}
如果(temp1!=NULL){free(temp1);}
出口(1);
打破
}
}
}
您的循环在的同时继续(trav2!=NULL)
。但是你在哪里设置下一步?否则,循环将永远继续
但是你的代码似乎有更多的问题。为什么要分配新的列表项?只需声明一个指向列表项(struct list*
)的指针,然后将其指向列表的标题
for (temp = HEAD; temp != NULL; temp = temp->next)
{
// Examine temp for the value you are looking for
}
我怀疑你对理解指针还是新手。这是使此代码正常工作的关键。在搜索例程中
for(struct list *trav1=trav2; strcmp(trav1->name,temp2->name)!=0;trav1=trav1->next);
在这里,trav1在列表的末尾将为空,但您仍在继续并取消它。
在for循环中添加检查temp1,如下所示:
for(struct list *trav1=trav2; trav1 && (strcmp(trav1->name,temp2->name)!=0);trav1=trav1->next);
或者,为了更好的可读性:
for (struct list *trav1 = trav2; trav1 != NULL; trav1 = trav1->next)
{
if (strcmp(trav1->name, temp2->name) !=0 )
break;
}
关于代码的其他一些注释:
fflush(stdin)
使用这样的东西,因为标准不保证它能正常工作
int c;
while ((c = getchar()) != EOF && c != '\n')
;
get()
。安全地使用get()
是不可能的。改为使用fgets()
(但要注意,它会在gets()处保留新行
将其丢弃)生成调试版本,并在调试器中运行。调试器将在崩溃的位置(最有可能是)停止,然后