主函数调运一个函数,那个函数一直循环运行,跳不出去,是怎么回事
#include <iostream>#include <stdio.h>
#include <string>
using namespace std;
struct Nodetype
{
int number; /*考生的考号*/
string name; /*考生姓名*/
string sex; /*考生性别*/
int age; /*考生年龄*/
string category; /*考生报考类别*/
Nodetype *next;
Nodetype *head;
};
class student
{
private:
Nodetype *head;
public:
student(){};
~student(){};
Nodetype *creat(); /*建立链表*/
void output(Nodetype *head); /*输出函数*/
void add(Nodetype *&head,Nodetype *stu); /*插入函数*/
void deleted(Nodetype *&head,int number); /*删除函数*/
void find(Nodetype *head,int key); /*查找函数*/
void modify(Nodetype *head,int key1); /*修改函数*/
};
Nodetype *student::creat() /*建立链表*/
{
int n;
cout <<"请输入考生人数:";
cin >>n;
cout <<"请依次输入考生的考号,姓名,性别,年龄及报考类别!"<<endl;
Nodetype *p1;
Nodetype *p2;
p1=new Nodetype;
cin >>p1->number >>p1->name >>p1->sex >>p1->age >>p1->category;
head=NULL;
p2=p1;
while(p1->number<n)
{
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=new Nodetype;
cin >>p1->number >>p1->name >>p1->sex >>p1->age >>p1->category;
}
p2->next=NULL;
delete p1;
return (head);
}
void student ::output(Nodetype *head) /*输出函数,输出链表内容*/
{
if(head=NULL)
{
cout <<"对不起,内容为空,不能输出"<<endl;
return;
}
cout <<"\t" <<"考号" <<"\t" <<"姓名" <<"\t"<<"性别"<<"\t" <<"年龄"<<"\t" <<"报考类别" <<endl;
while(head)
{
cout <<"\t" <<head->number <<"\t" <<head->name <<"\t" <<head->sex <<"\t" <<head->age <<"\t" <<head->category <<endl;
head=head->next;
}
}
void student ::add(Nodetype *&head,Nodetype *stu) /*插入操作*/
{
if(head=NULL)
{
head=stu;
stu->next=NULL;
}
if(head->number>stu->number)
{
stu->next=head;
head=stu;
}
Nodetype *p=head;
while(p->next&&p->next->number<stu->number)
p=p->next;
stu->next=p->next;
p->next=stu;
}
void student ::deleted(Nodetype *&head,int number) /*删除操作*/
{
Nodetype *p1;
if(!head)
{
cout <<"空表,不能进行删除操作" <<endl;
return;
}
if(head->number==number)
{
p1=head;
head=p1->next;
delete p1;
cout <<"你删除的考生信息是:" <<"\t" <<p1->number <<"\t" <<p1->name <<"\t" <<p1->sex <<"\t" <<p1->age <<"\t" <<p1->category <<endl;
return;
}
for(Nodetype *p2=head;p2->next;p2=p2->next)
{
if(p2->next->number==number)
{
p1=p2->next;
p2->next=p1->next;
delete p1;
cout <<"你删除的考生信息是:" <<"\t" <<p1->number <<"\t" <<p1->name <<"\t" <<p1->sex <<"\t" <<p1->age <<"\t" <<p1->category <<endl;
return;
}
}
cout <<"没有该考生"<<"\n";
return;
}
void student::find(Nodetype *head,int key) /*查找操作*/
{
Nodetype *p;
if(head=NULL)
{
cout <<"对不起,内容为空,不能输出"<<endl;
return;
}
if(head->number==key)
{
cout <<"你查找的学生信息为:" <<endl;
cout <<"\t" <<"考号" <<"\t" <<"姓名" <<"\t"<<"性别"<<"\t" <<"年龄"<<"\t" <<"报考类别" <<endl;
cout <<"\t" <<head->number <<"\t" <<head->name <<"\t" <<head->sex <<"\t" <<head->age <<"\t" <<head->category <<endl;
return;
}
for(p=head;p->next;p=p->next)
{
if(p->next->number==key)
{
cout <<"你查找的学生信息为:" <<endl;
cout <<"\t" <<"考号" <<"\t" <<"姓名" <<"\t"<<"性别"<<"\t" <<"年龄"<<"\t" <<"报考类别" <<endl;
cout <<"\t" <<p->next->number <<"\t" <<p->next->name <<"\t" <<p->next->sex <<"\t" <<p->next->age <<"\t" <<p->next->category <<endl;
return;
}
}
cout <<"没有该考生"<<endl;
}
void student::modify(Nodetype *head,int key1)
{
Nodetype *p;
Nodetype *p1;
p=new Nodetype;
if(head=NULL)
{
cout <<"对不起,内容为空,不能输出"<<endl;
return;
}
if(head->number==key1)
{
cout <<"你要修改的学生原有信息为:" <<endl;
cout <<"\t" <<head->number <<"\t" <<head->name <<"\t" <<head->sex <<"\t" <<head->age <<"\t" <<head->category <<endl;
cout <<"请输入修改后的信息:"<<endl;
cin >>p->number>>p->name>>p->sex>>p->age>>p->category ;
head=p;
return;
}
for(p1=head;p1->next;p1=p1->next)
{
if(p1->next->number==key1)
{
cout <<"你要修改的学生原有信息为:" <<endl;
cout <<"\t" <<p1->next->number <<"\t" <<p1->next->name <<"\t" <<p1->next->sex <<"\t" <<p1->next->age <<"\t" <<p1->next->category <<endl;
cout <<"请输入修改后的信息:"<<endl;
cin >>p->number>>p->name>>p->sex>>p->age>>p->category ;
p1->next=p;
return;
}
}
cout <<"没有该考生"<<endl;
}
void main()
{
Nodetype *head=NULL;
student person;
int number,key,key1;
char c;
char choice;
cout <<"首先请建立考试信息系统!" <<endl;
person.creat();
head=person.creat(); /*就是这个函数!!!!!!!!!!!!!!!!!!11*/
person.output(head);
}
如上代码,我在调运创建链表的函数时,该函数一直循环运行,跳不出去。