求C语言大神看看这段链表的代码,改一下错。

2025-05-20 21:34:05
推荐回答(2个)
回答(1):

//可以运行,LZ果断加分吧。。。。。。。。。
#include
#include
#include
struct Node{
int age;
char name[10];
struct Node * next;
};

int main(){
int k,i,a;
Node * head,* cur,* thisN;
head = NULL;
char name[10];

for(i=1;;i++) {
cur=new Node();
if(head==NULL)
head=cur;
else
{
thisN=head;
while(thisN->next!=NULL)
thisN=thisN->next;
thisN->next=cur;
}

thisN=cur;
printf("1:");
if(scanf("%s",thisN->name),strcmp("over",thisN->name)==0) //问题一
{thisN->next=NULL; break;} //问题二
printf("2:");
scanf("%d",&k);
thisN->age=k;
thisN->next=NULL;
}

k=25; cur=head;
while((cur!=NULL)&&(cur->age!=k))
{
cur=cur->next;
}
if(cur!=NULL)
printf("%s",cur->name);
else
printf("No found");

while(head!=NULL) {
cur=head;
head=cur->next;
delete cur;
}

return 0;}

回答(2):

(scanf("%s",thisN->name))=="over"//不能这么写,scanf的返回值是成功赋值的数据数量,是整数型,不能和字符串比较