求改错,一个链表的代码,谢谢
#include<iostream.h>#include<string.h>
struct STUDENT
{
char num[10];
int score;
STUDENT *next;
}
STUDENT *Create() //新建链表
{
STUDENT *p,*q,*head;
int i=1,number;
cout<<"请输入学生人数:"<<endl;
cin>>number;
cout<<"请按学号顺序输入各位同学的学号和成绩!"<<endl;
head=0;
while(i<=number)
{
p=new STUDENT;
cout<<"请输入第"<<i<<"位同学的学号和成绩:";
cin>>p->num;
cin>>p->score;
if(head==0)
{
head=p;
q=p;
}
else
{
q->next=p;q=p;
}
i++;
}
q->next=0;
return(head);
}
void Travel(STUDENT *head) //遍历链表
{
STUDENT *p;
int i=1;
p=head;
cout<<"链表中的数据输出如下:"<<endl;
while(p!=0)
{
cout<<"第"<<i<<"个节点中的数据为"<<endl;
cout<<p->num<<" "<<p-score<<endl;
p=p->next;
i++;
}
}
STUDENT *Insertnode(STUDENT *head,STUDENT *p) //在链表中插入一个节点
{
STUDENT *be,*ne;
if(head==0)
{
head=p;
p->next=0;
return(head);
}
if(strcmp(head->num,p->num)>=0)
{
p->next=head;
head=p;
return(head);
}
be=ne=head;
while(ne!=0 && strcmp(ne->num,p->num)<0)
{
be=ne;
ne=ne->next;
}
p->next=ne;
be->next=p;
return(head);
}
STUDENT *Deletenode(STUDENT *head,char num[]) //在链表中删除一个节点
{
STUDENT *be,*p;
if(head==NULL)
{
cout<<"链表已经为空,无节点可以删除!"<<endl;
return(head);
}
if(strcmp(head->num,num)==0)
{
p=head;
head=head->next;
delete p;
}
else
{
be=p=head;
while(strcmp(p->num,num)!=0 && p->next!=NULL)
{
be=p;
p=p->next;
}
if(strcmp(p->num,num)==0)
{
be->next=p->next;
delete p;
cout<<"删除了一个节点!"<<endl;
}
else
cout<<"链表中没有找到所要删除的节点!"<<endl;
}
return(head);
}
void releaschain(STUDENT *head) //释放链表
{
STUDENT *p;
while(head)
{
p=head;
head=head->next;
delete p;
}
}
void main(void)
{
STUDENT *head,*t;
char n[10];
head=Create();
Travel(head);
t=new STUDENT;
cout<<"请输入需要插入同学的学号和成绩:";
cin>>t->num>>t->score;
head=Insertnode(head,t);
Travel(head);
cout<<"请输入要删除同学的学号:";
cin>>n;
head=Deletenode(head,n);
Travel(head);
releasechain(head);
}