标题:[原创]单链表的冒泡排序
取消只看楼主
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
 问题点数:0 回复次数:0 
[原创]单链表的冒泡排序

#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(); }

搜索更多相关主题的帖子: Node 单链 head item 
2005-05-12 10:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-17955-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.423189 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved