Y
ytbszl80
昨天闲着没事想写个有关list的程序!!要实现输入一个随机链表,然后多他进行排序输出,可是排序的部分怎么都运行不正确,有请哪位高手看看!!
源代码如下:
#include
#include
void main()
{typedef struct node
{int data; struct node *next;} Nodetype;
Nodetype *head,*tail,*swap,*swap2,*pr;
int num,i,input,flag=1;
head=new Nodetype;
head=NULL;
tail=new Nodetype;
swap2=new Nodetype;
pr=new Nodetype;
cout<<"please input the node number:"<>num;
for (i=0;i < num;i++)
{cout<<"please input the "<>input;
swap=new Nodetype;
if(head==NULL)
{swap->data=input;
head=swap;
tail=head;
}
else
{swap->data=input;
tail->next=swap;
tail=tail->next;
}
}
swap2=head;
for (i=0;idata<next;
}
getch();
pr=head;
swap=head;
swap2=head->next;
while(swap2!=tail)
{if(swap2->data < swap->data)
{if(flag)
{swap->next=swap2->next;
swap2->next=swap;
head=swap2;
swap2=swap->next;
flag=0;
}
else
{pr=head;
if(head->data > swap2->data)
{swap->next=swap2->next;swap2->next=head;head=swap2;swap2=swap->next;}
else
{while(swap2->data > pr->next->data) pr=pr->next;
swap->next=swap2->next;swap2->next=pr->next;pr->next=swap2;swap2=swap->next;
}
}
}
else
{swap=swap2;swap2=swap2->next;}
}
pr=head;
if(head->data > swap2->data)
{swap->next=swap2->next;swap2->next=head;head=swap2;swap2=swap->next;}
else
{while(swap2->data > pr->next->data) pr=pr->next;
swap->next=swap2->next;swap2->next=pr->next;pr->next=swap2;swap2=swap->next;
}
swap=head;
while(swap!=tail)
{cout<data<next;}
cout<data;
delete swap;delete pr;delete swap2;delete tail;delete head;
}
谢谢!!