各位帮我看看呀。。 我是照的书在写呀。。 怎么会是死循环呢。
#include "stdafx.h"
#include "iostream.h"
#include "string.h"
struct student
{
char num[5];
char name[20];
char tel[15];
student *next;
};
int n;
student* creat()
{
student *head,*p1,*p2;
n=0;
p1=p2=new student;
cout<<"input num name tel"<<endl;
cin>>p1->num>>p1->name>>p1->tel;
head=NULL;
while(p1->num!=0) //从这里开始就是死循环了,没有办法退出while 语句?
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=new student;
cin>>p1->num>>p1->name>>p1->tel;
}
p2->next=NULL;
cout<<"creat successful"<<endl;
return(head);
}
student* delstu(student*head)
{
student *p1,*p2;
int i,m;
char number[5];
cout<<"please input a number.xuhao=1 xuehao=2 tuichu=3"<< endl;
while(m<3)
{
cin>>m;
if(m==1)
{
cin>>i;
if(head==NULL)
{
cout<<"list null"<<endl;
return head;
}
p1=head;
while(p2!=NULL&&i!=0)
{
i--;
p2=p1;
p1=p1->next;
}
if(i==0)
{
p2->next =p1->next ;
delete p1;
n--;
cout<<"delete successful"<<endl;
return head;
}
else
{
cout<<"not been found"<<endl;
return head;
}
}
else if(m==2)
{
cin>>number;
if(head==NULL)
{
cout<<"list null"<<endl;
return head;
}
p1=head;
while(p2!=NULL&&strcmp(p1->num,number))
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->num ,number))
{
p2->next =p1->next ;
delete p1;
n--;
cout<<"delete successful"<<endl;
return head;
}
else
{
cout<<"not been found"<<endl;
return head;
}
}
}
}
int main(int argc, char* argv[])
{
student *head;
head=creat();
head=delstu(head);
return 0;
}