这是关于链表的程序,出现了问题,不知哪出错,请求帮助!
#include<iostream>using namespace std;
#define NULL 0
struct student
{
int num;
student*next;
};
int n;
int main()
{ student*creat(void);
student*insert(student*,student*);
student*del(student*,int);
void print(student*);
student*head;
student*stu;
int del_num;
cout<<"input num:"<<endl;
head=creat();
print(head);
cout<<endl<<"input the inserted record:";
stu=new student;
cin>>stu->num;
while(stu->num!=0)
{
head=insert(head,stu);
print(head);
cout<<endl<<"input the inserted record:";
stu=new student;
cin>>stu->num;
}
cout<<endl<<"input the deleted number:";
cin>>del_num;
while(del_num!=0)
{
head=del(head,del_num);
print(head);
cout<<endl<<"input the deleted number:";
cin>>del_num;
}
return 0;
}
/*……………………………………………………主函数………………………………………………………………*/
student*creat(void)
{student*head;
student*p1,*p2;
n=0;
p1=p2=new student;
cin>>p1->num;
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=new student;
cin>>p1->num;
}
p2->next=NULL;
return(head);
}
student*insert(student*head,student*stud)
{student*p0,*p1,*p2;
p1=head;
p0=stud;
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next=p0;
}
if(p0->num<=p1->num)
{
if(head=p1)
head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;p0->next=NULL;
}
return(head);
}
student*del(student*head,int num)
{student*p1,*p2;
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1=head)
head=p1->next;
else p2->next=p1->next;
cout<<"delete:"<<num<<endl;
}
else cout<<"It doesn't exist!";
return(head);
}
void print(student*head)
{student*p;
cout<<"Now these num are:"<<endl;
p=head;
if(p!=NULL)
p=p->next;
cout<<p->next;
}
这个是写关于链表的程序,要对链表中的各个结点的数按从小到大的顺序排列,然后再删去一个数后输出,对排序部分有点乱,我采用的是输入一个排一个的方法。麻烦帮我看一下!