| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1030 人关注过本帖
标题:[原创]单链表的冒泡排序
取消只看楼主 加入收藏
热情依然
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
快速回复:[原创]单链表的冒泡排序
数据加载中...
 
   



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

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