各位大神可以看看下面代码有哪些错误,自己动手搞得,好像不太成功但是自己没有能力改。
#include<iostream>
using namespace std;
//构造一个存放学生信息(姓名、年龄、性别、学号、成绩)的链表
//主要为增加、修改、删除、查询这四个基本功能,后来懒得写修改
struct student
{
char name[20];
int age;
char sex;
long long num;//学号
float score;
student* next;
};
//创建一个链表
void creatstudent(student*head,int n)
{
student* end = 0;//尾结点
head = 0;
for (int i = 0; i < n; i++)
{
student* p = new student;//堆空间动态创建链表结点
cin >> p->name >> p->age >> p->num >> p->score >> p->sex;
if (head == 0)
head = p;//空链表
else//非空链表
end->next = p;
end = p;
p->next = 0;
}
}
//增加学生信息,尾插法
void addstudent(student*head)//先将结点插入链表中(判断一下链表是否为空,因为传入头结点数据,利用头结点判断),然后输入具体内容。
{
student* end=NULL,*p;
p = new student;
cout << "请输入姓名:" << endl;
cin >> p->name;
cout << "请输入年龄:" << endl;
cin >> p->age;
cout << "请输入性别:" << endl;
cin >> &p->sex;
cout << "请输入学号:" << endl;
cin >>p->num;
cout << "请输入分数:" << endl;
cin >> p->score;
if (head == 0)
{
head = p->next;
p = head;
}
else
{
p->next = NULL;
end->next = p;
}
cout << "存储完毕!";
}
//删除学生信息
void shanchu(student* head,int i)//i表示删除第i个的信息
{
student* end, * p;
end = new student;
p = new student;
if (i == 1)//链表头结点单独处理
{
p = head;//p指向待删除的结点
head = head->next;
delete p;
}
else//非头结点
{
p = head;
for (int n = 1; n < i - 1; n++)//p指向第i-1个结点
p = p->next;
end = p->next;//end指向待删除的结点
p->next = end->next;
delete end;
}
}
//查询学生信息
void chaxun(int sum)//按学号查找
{
student* p;
p = new student;
if (sum == p->num)
{
cout << p->name << p->age << p->num << p->sex << p->score << '\t';
}
else
{
cout << "未找到。";
}
}
int main()
{
int m;
student* p=0;
while (1)
{
cout << endl;
cout << "----------------1-------增加学生信息----------------" << endl;
cout << "----------------2-------删除学生信息----------------" << endl;
cout << "----------------3-------查找学生信息--------------" << endl;
cout << endl;
cout << "请输入想要的操作:" << endl;
cin >> m;
switch (m)
{
case 1:
addstudent(p);
break;
case 2:
int i;
cout << "请输入想要删除第i个学生信息:";
cin >> i;
shanchu(p, i);
break;
case 3:
int a;
cout << "请输入要查询的学号:";
cin >> a;
chaxun(a);
break;
}
}
system("pause");
}
就比如下面那个,为什么保存了查询时却查询不到?
![](https://i-blog.csdnimg.cn/blog_migrate/81bdd9d58cbbc22508ec88fad180a44a.png)