#include
#include
#include
using namespace std;
struct student
{
char name[20];
char num[20];
int math;
int english;
int py;
int sum;
int average;
};
struct node
{
struct student data;
struct node*next;
};
struct node*headnode;
struct node* createhead()
{
struct node*headnode=(struct node*)malloc(sizeof(struct node));
assert(headnode);//判断是否申请内存成功
headnode->next=NULL;
return headnode;
}
struct node* createnode(struct student data)
{
struct node*newnode=(struct node*)malloc(sizeof(struct node));
assert(newnode);//判断是否申请内存成功
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void insertnode(struct node*headnode,struct student data)//headnote只需要,createnode函数只用调用一次 ,后面只需传入头节点就行
{
struct node*newnode=createnode(data);
newnode->next=headnode->next;
headnode->next=newnode;
}
void printlist(struct node*headnode)
{
struct node*passnode=headnode->next;
cout<<"姓名"<<"\t" <<"编号"<<"\t" <<"数学"<<"\t" <<"英语"<<"\t"<<"py"<<"\t"<<"总分"<<"\t"<<"平均分"<<"\t";
while(passnode!=NULL)
{
cout<data.name<data.num<data.math<data.english<data.py<data.sum<data.average<<"\t";
passnode=passnode->next;
}
}
void makemenu()
{
cout<<"---------链式学生管理系统----------"<"1.录入功能"<"2.浏览功能"<"3.查找功能"<"4.修改功能"<"5.删除功能"<"6.退出系统"<"-----------------------------------"<void deletedata(struct node*headnode,const char*name)
{
struct node*prenode=headnode;
struct node*curnode=headnode->next;
while(curnode!=NULL&&strcmp(curnode->data.name,name))
{
prenode=curnode;
curnode=prenode->next;
}
if(curnode=NULL)
{
cout<<"删除失败,没有找到指定数据"<else
{
prenode->next=curnode->next;
free(curnode);
}
}
struct node*searchdata(struct node*headnode,const char*name)
{
struct node*pmove=headnode->next;
while(pmove!=NULL&&strcmp(pmove->data.name,name))
{
pmove=pmove->next;
}
return pmove;
}
void keydown()
{int m;
cin>>m;
struct student temp;
struct node*result;
switch(m)
{
case 1:cout<<"----【录入模块】----"<"请输入学生信息:"<"姓名"<<"\t" <<"编号"<<"\t" <<"数学"<<"\t" <<"英语"<<"\t"<<"py"<>temp.name>>temp.num>>temp.math>>temp.english>>temp.py;
temp.sum=temp.math+temp.english+temp.py;
temp.average=temp.sum/3;
insertnode(headnode,temp);
break;
case 2:cout<<"----【浏览模块】----"<printlist(headnode);
break;
case 3:cout<<"----【查找功能】----"<"请输入要查找的名字:";
cin>>temp.name;
searchdata(headnode,temp.name);
result=searchdata(headnode,temp.name);
if(result==NULL)
{
cout<<"未找到结果";
}
else
{
cout<<"姓名"<<"\t" <<"编号"<<"\t" <<"数学"<<"\t" <<"英语"<<"\t"<<"py"<<"\t"<<"总分"<<"\t"<<"平均分"<<"\t";
cout<data.name
<data.num
<data.math
<data.english
<data.py
<data.sum
<data.average<<"\t";
}
break;
case 4:cout<<"----【修改功能】----"<"请输入要修改的名字:";
cin>>temp.name;
searchdata(headnode,temp.name);
result=searchdata(headnode,temp.name);
if(result==NULL)
{
cout<<"未找到结果";
}
else
{cout<<"输入新同学信息:" ;
cout<<"姓名"<<"\t" <<"编号"<<"\t" <<"数学"<<"\t" <<"英语"<<"\t"<<"py"<<"\t"<<"总分"<<"\t"<<"平均分"<<"\t";
scanf("%s%s%d%d%d",result->data.name,20,result->data.num,20,&result->data.math,&result->data.english,&result->data.py);
result->data.sum=result->data.math+result->data.english+result->data.py;
result->data.average=result->data.sum/3;
}
break;
case 5:cout<<"----【删除功能】----"<"请输入要删除的学生姓名:";
cin>>temp.name ;
deletedata(headnode,temp.name);
break;
case 6:cout<<"----【退出系统】----"<exit(0);
break;
default:
cout<<"输入错误,请重新输入!"<break;
}
}
int main()
{ struct node*headnode=createhead();
while(1)
{
makemenu();
keydown();
system("pause");
system("cls");
}
return 0;
}
为啥这会直接退出程序啊,求解
输出显示程序崩溃了
178行, struct node*headnode=createhead();改为 headnode=createhead();
因为headnode已经是全局变量了,你这里又定义一个局部变量。导致实际全局变量headnode并没有初始化,所以其它函数使用headnode就会崩溃