//以下代码已测试
#include<iostream>
#include<stdlib.h>
using namespace std;
struct Queue
{
int x;
Queue*next;
};
void Swap(int &a,int &b)
{
int temp;temp=a;a=b;b=temp;
}
void Descend(Queue*h)//引入头指针
{
Queue*last;last=NULL;
Queue*p;
do
{
p=h;
while(p->next!=last)
{
if(p->x<p->next->x)
Swap(p->x,p->next->x);
p=p->next;
}
last=p;
}while(h->next!=last);
}
Queue*head;
int main()
{
cout<<"请输入整数存入链表,以-10000结束输入。\n";
int get;
Queue*p_new;Queue*p;p=head;
while(true)
{
cin>>get;
if(get==-10000) break;
else
{
p_new=new Queue;
p_new->x=get;p_new->next=NULL;
if(p==NULL)
{
p=p_new;head=p;
}
else
{
p->next=p_new;p=p->next;
}
}
};
cout<<"原始顺序:\n";
p=head;
while(p!=NULL)
{
cout<<p->x<<endl;p=p->next;
}
p=head;
cout<<"新顺序:\n";
Descend(head);
while(p!=NULL)
{
cout<<p->x<<endl;p=p->next;
}
system("pause");
return 0;
}
//代码有点长,只看Descend函数就行了,Swap是将两数置换,其他都只是输入