我的这个关于链表的程序不能排序,只能输入数不能输出排好的数。麻烦帮我改一下。谢谢啦!
#include<iostream>using namespace std;
#define NULL 0
struct su
{int num;
su *next;
};
int main()
{su *creat(void);
su *insert(su*,su*);
su *del(su*,int);
void print(su*head);
su*head,*stu;
int del_num;
cout<<"input records:"<<endl;
head=creat();
print(head);
cin>>"the records";
print(head);
cout<<endl<<"input the inserted record:";
stu=new su;
cin>>stu->num;
while(stu->num!=0)
{head=insert(head,stu);
print(head);
cout<<endl<<"input the inserted record:";
stu=new su;
cin>>stu->num;
}
cout<<endl<<"input the deleted number:";
cin>>del_num;
while(del_num!=0)
{head=del(head,del_num);
print(head);
cout<<"input the deleted number:";
cin>>del_num;
}
return 0;
}
/* ..............建立链表..............*/
su *creat(void)
{su *head,*p,*q;
p=q=new su;
cin>>p->num;
while(p->num!=0)
{
if (head==NULL)
{
head=p;
q=head;
}
else
{
q->next=p;
q->next=NULL;
}
q=p;
p=new su;
cin>>p->num;
}
//q->next=NULL;
return(head);
}
/*......................插入.........................*/
su *insert(su*head,su*stu)
{su*p0,*p1,*p2;
p1=head;
p0=stu;
if(head=NULL)
{ head=p0;p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1; p1=p1->next;}
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);
}
/*......................删除......................*/
su *del(su *head,int num)
{su *p1,*p2;
if(head=NULL)
cout<<"list null"<<endl;
p1=head;
while(num!=p1->num&&p1->num!=NULL)
{
p2=p1;p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)head=p1->next;
else p2->next=p1->next;
cout<<"deleted:"<<num<<endl;
}
else cout<<"no find"<<num;
return (head);
}
/*..............输出...................*/
void print(su*head)
{ su *p1,*p2;
int temp;
//p1=head;
for( p1=head ;p1!=NULL;p1=p1->next)
for(p2=p1->next; p2!=NULL;p2=p2->next)
if(p1->num>p2->num)
{
temp=p1->num;
p1->num=p2->num;
p2->num=temp;
}
//{cout<<p1->num<<endl;
//p1=p1->next;}
p1=head;
do
{
cout<<p1->num<<endl;
p1=p1->next;
} while (p1!=NULL);
}
我的这个关于链表的程序不能排序,只能输入数不能输出排好的数。