C语言链表如何实现冒泡排序?

给我一种简单的示例即可。。。
typedef struct node{
int data;
struct node*link;
}LNode,*LinkList;

第1个回答  推荐于2017-11-25
两种 思路:
1、交换链表中各节点的数据,不交换节点;
2、交换节点,即节点link指针的内容。
下面以第一种思路为例:
void sort(LinkList *L)
{
int i,j,t;
LinkList *p = L;
int count = 0;
while(p->link != NULL)
{
count++;
p = p->link;
}
for(i=0;i<count-1;i++) /* 冒泡法排序 */
{
p = L;
for(j=0;j<count-i-1;j++)
{
if(p->data > p->link->data)
{
t=p->data;
p->data = p->link->data;
p->link->data=t;
}
p = p->link;
}
}
}本回答被提问者采纳
第2个回答  2012-12-13
#include<stdio.h>
#include<malloc.h>
struct number
{
int num;
struct number *next;
};
void main()
{
struct number *head;
struct number *p1,*p2,*p,*p3,*p4;
int n=0,m,i,j;
p1=p2=(struct number *)malloc(sizeof(struct number));
printf("请输入整数,输入0结束: \n");
scanf("%d",&p1->num);
head=NULL;
while(p1->num!=NULL)
{
n=n+1;
if(head==NULL) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct number *)malloc(sizeof(struct number));
scanf("%d",&p1->num);
}
p2->next=NULL;
p=head;
for(p4=head;p4!=NULL;p4=p4->next)
{
for(p3=head;p3->next!=NULL;p3=p3->next)
{

if(p4->num<p3->num)
{

m=p4->num;
p4->num=p3->num;
p3->num=m;

}

}
p=head;
}
printf("\n:冒泡从小到大排序后的结果:\n");
p=head;
printf("\n");
if(head!=NULL)
do
{
printf("%5d ",p->num);
p=p->next;
}while(p!=NULL);
printf("\n");
getchar();
}
//不懂再问。。。。测试通过本回答被网友采纳