#include<stdio.h> #include<stdlib.h>
typedef struct node{ int data; struct node *next; }Node,*LinNode; LinNode Creat() { LinNode head=NULL,p; int i,n,item; printf("intput n:\n"); scanf("%d",&n); printf("input data:\n"); for(i=0;i<n;i++) { fflush(stdin); scanf("%d",&item); p=(Node*)malloc(sizeof(Node)); p->data=item; p->next=head; head=p; } return head; } LinNode bubblesort(LinNode head){ LinNode p,q,tail,h; h=(Node*)malloc(sizeof(Node)); h->next=head; tail=NULL; while(h->next!=tail) { p=h; q=p->next; while(q->next!=tail) { if(p->next->data>q->next->data) { p->next=q->next; q->next=q->next->next; p->next->next=q; p=p->next; } else{q=q->next; p=p->next; } } tail=q; } head=h->next; free(h); return head; }
void output(LinNode head){ LinNode p; p=head; while(p){ printf("%d\t",p->data); p=p->next; } printf("\n"); } void main() { LinNode head; head=Creat(); printf("output the numbers:\n"); output(head); printf("\n"); printf("output the after numbers:\n"); head=bubblesort(head); output(head); getch(); }