#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0 //宏定义NULL为0
#define SIZ sizeof(struct stu) //宏定义 SIZ为sizeof(struct stu)
struct stu //结构体
{
int num;
stu *next;
};
void main()
{
stu *H=NULL; //定义头指针
stu *r,*s,*p,*q;
int x,y;
cin>>x;
r=NULL; //建立单链表A
while(x!=NULL)
{
s=(stu*) malloc(SIZ);
s->num=x;
if(H==NULL)
H=s;
else
r->next=s;
r=s;
cin>>x;
}
if(r!=NULL)
r->next=NULL;
r=H;
while(r!=NULL) //输出单链表A
{
cout<<r->num<<" ";
r=r->next;
}
cout<<endl;
cin>>y; //输入要插入的数据
r=H;
p=H; //指向头结点
s=(stu*) malloc(SIZ); //申请空间
s->num=y;
if(H==NULL) //头结点为空,使头结点指向S
H=s;
else //头结点不为空,插入S
while(p->num<s->num)
{
if(p->next->num>s->num)
{
q=p->next;
s->next=q;
p->next=s;
}
p=p->next;
}
r=H;
while(r!=NULL)
{
cout<<r->num<<" ";
r=r->next;
}
cout<<endl;
}