1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include<malloc.h>
typedef struct { int data; struct *next; } Node,*Linklist;
void IniList(Linklist *L) { *L=(Linklist)malloc(sizeof(Node)); (*L)->next=NULL; }
void CreList(Linklist *L) { int i,n,t; Linklist r,s; IniList(L); r=*L; scanf("%d",&n); for(i=1; i<=n; i++) { scanf("%d",&t); s=(Linklist)malloc(sizeof(Node)); s->data=t; s->next=r->next; r->next=s; r=s; }
}
void maopao(Linklist L) { Linklist pre, p, tail, temp; tail=NULL; pre=L; Linklist head = L; while((head->next->next)!=tail) { p=head->next; pre=head; while(p->next!=tail) { if((p->data)>(p->next->data)) {
pre->next=p->next; temp=p->next->next; p->next->next=p; p->next=temp; p=pre->next;
} p=p->next; pre=pre->next; } tail=p; } }
void PriList(Linklist L) { Linklist p=L->next; while(p) { printf("%d ",p->data); p=p->next; }
} int main() { Linklist L; CreList(&L); maopao(L); printf("n"); PriList(L); return 0; }
|
近期评论