输入:5
2 3 4 1 5
输出:1 2 3 4 5
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct st
{
int date;
struct st *next;
}linklist;
linklist* creat(int n)
{
linklist* head,*tail,*p;
head=(linklist*)malloc(sizeof(linklist));
head->next=NULL;
tail=head;
int i;
for(i=0;i<n;i++)
{
p=(linklist*)malloc(sizeof(linklist));
scanf("%d",&p->date);
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
linklist*sort(linklist *L) //一般的冒泡排序
{
linklist*p,*q;
for(p=L->next;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->date>q->date)
{
int t=p->date;
p->date=q->date;
q->date=t;
}
}
}
return L;
}
void pre(linklist*L)
{
linklist*p=L->next;
int n=0;
while(p)
{
n++;
if(n==1)
printf("%d",p->date);
else
printf(" %d",p->date);
p=p->next;
}
}
int main()
{
linklist *L;
int n;
scanf("%d",&n);
L=creat(n);
L=sort(L);
pre(L);
return 0;
}