64,677
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
#define LEN sizeof(struct Number)
struct Number
{
int num;
struct Number *next;
};
int main(){
struct Number *head,*p,*q;
int x,i,j,temp;
head=p=q=(struct Number *)malloc(LEN);
cin>>x;
for(i=0;;i++)
{
p->next=(struct Number *) malloc(LEN);
p=p->next;
p->num=x;
cin>>x;
if(x==0)break;
}
p->next=NULL;
q = head->next;
for(j=1;j<i;++j){
p = q;
while(q && q->next != NULL){
if(q->num > q->next->num){
temp = q->num;
q->num = q->next->num;
q->next->num = temp;
}
q = q->next;
}
q = p->next;
}
p=head->next;
while(p!=NULL)
{
cout<<p->num<<" ";
p=p->next;
}
cout<<endl;
p = q = head;
while(p){
q = p->next;
free(p);
p = q;
}
system("pause");
return 0;
}