链表哪里出错了,该怎么写?
#include <iostream>using namespace std;
struct student
{
char name;
int age;
student* next;
};
int main()
{
int i = 0;
int bfound = 0;
student c = {"Kaka",23,NULL};
student b = {"Deco",27,&c};
student a = {"Terry",30,&b};
student* head = &a;
student* pointer = head;
char name = {0};
cout<<"请输入学生姓名"<<endl;
cin>>name;
while(pointer != NULL)
{
if(strcmp(pointer->name,name) == 0)
{
bfound = 1;
break;
}
pointer = pointer->next;
}
if(bfound)
{
cout<<"name"<<pointer->name<<"age"<<pointer->age<<endl;
}
else
{
cout<<"no found"<<endl;
}
return 0;
} 〃忝書γě渎ぐ 发表于 2020-2-25 16:37
请输入学生姓名
小明
no found
你的链表里面本身就没有小明啊。{:10_243:}
你的链表只有三个结点,姓名分别为Terry, Deco, Kaka。你输这三个中的某一个试试。
你这个跟链表怎么实现没啥关系。
你都不看报错信息的吗?
error: 'strcmp' was not declared in this scope
if(strcmp(pointer->name,name) == 0)
note: 'strcmp' is defined in header '<cstring>'; did you forget to '#include <cstring>'?
main.cpp:2:1:
这些都是在提醒你要用strcmp函数需要增加头文件ctring。
#include <iostream>
#include<cstring> // strcmp()函数在这个头文件
using namespace std;
struct student
{
char name;
int age;
student* next;
};
int main()
{
int i = 0;
int bfound = 0;
student c = {"Kaka",23,NULL};
student b = {"Deco",27,&c};
student a = {"Terry",30,&b};
student* head = &a;
student* pointer = head;
char name = {0};
cout<<"请输入学生姓名"<<endl;
cin>>name;
while(pointer != NULL)
{
if(strcmp(pointer->name,name) == 0)
{
bfound = 1;
break;
}
pointer = pointer->next;
}
if(bfound)
{
cout<<"name: "<<pointer->name<<", age: "<<pointer->age<<endl; //这里帮你改了一下打印格式
}
else
{
cout<<"no found"<<endl;
}
return 0;
} major_lyu 发表于 2020-2-25 16:34
你这个跟链表怎么实现没啥关系。
你都不看报错信息的吗?
请输入学生姓名
小明
no found
每次都是no found,那有没有什么逻辑上的错误?
major_lyu 发表于 2020-2-25 16:34
你这个跟链表怎么实现没啥关系。
你都不看报错信息的吗?
大神,你C++怎么学的,能不能说说?给点建议。
页:
[1]