c语言如何对链表的数进行排序

同学,给你一段代码,里面涵盖了链表的冒泡排序!
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;/*data代表成绩分数*/
struct node *next;
}LNode,*LinkList;
LinkList Creat(void)/*创建链表,结束标志为当输入的数据为0!*/
{
LinkList H,p1,p2;
int n;
n=0;
p1=p2=(LinkList)malloc(sizeof(LNode));
printf("输入数据:");
scanf("%d",&p1->data);
H=NULL;
while(p1->data!=0)
{
n=n+1;
if(n==1)
H=p1;
else
p2->next=p1;
p2=p1;
p1=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p1->data);
}
p2->next=NULL;
return(H);
}
LinkList Sort(LinkList SL)/*递增排序函数:入口参数:链表的头指针,此为链表中的排序函数*/
{
LinkList p,q;
int temp;
for(p=SL;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->data>q->data)
{
temp=q->data;
q->data=p->data;
p->data=temp;
}
}
}
return SL;
}

int main()
{
LinkList L,S,K;
L=Creat();
printf("初始化的单链表数据序列为:\n");
for(S=L;S!=NULL;S=S->next)
printf("%d ",S->data);
Sort(L);
printf("\n按递增顺序排序后的序列为:\n");
for(K=L;K!=NULL;K=K->next)
printf("%d==>",K->data);

return 0;
}

Ⅱ C语言 链表如何进行排序!

//排序( 参数为链表头 )
void Sorting( student* pHead )
{
//参数判断(如果是空就返回)
if ( pHead == NULL )
{
return;
}

//临时变量..做冒泡循环用..
student* pTempA = pHead;
student* pTempB = pHead;
//这两个while相当于冒泡的嵌套for循环
while( pTempA != NULL )
{
while ( pTempB != NULL )
{
//判断结构体里面int类型(学号)大小
if ( pTempA->iNum < pTempB->iNum )
{
//将结构体里int类型(学号)值进行交换
int Num = pTempA->iNum;
pTempA->iNum = pTempB->iNum;
pTempB->iNum = Num;

//将char类型(名字)做交换
char Name[ 16 ];
strcpy ( Name, pTempA->strName );
strcpy ( pTempA->strName, pTempB->strName );
strcpy ( pTempB->strName, Name );

//因为指针变量不做交换...所以不做结构体的直接交换
//除了指向下一个结点的指针变量外所有的值进行交换
}
//做链表遍历(相当于循环的i++)
pTempB = pTempB->pNext;
}
//因为for每次进来i = 0; 这里while循环结束..要让pTempB重新回到头..
pTempB = pHead;
//做链表遍历(相当于循环的i++)
pTempA = pTempA->pNext;
}
}

连接晚上回来给你写...

Ⅲ C语言做链表的排序

主要修改了sort函数,采用冒泡排序算法进行排序的。
你其他的两个函数写的不错,就sort函数写的有问题,已经很不错了。
注意:程序结束,最好对链表进行销毁,否则,内存永远也不会释放,导致内存泄漏了。

修改如下:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#define N 5
typedef struct node
{
char name[20];
int score;
struct node *link;
}stud;

stud *sort(stud *head) /*排序函数*/
{
stud *temp=NULL; //默认为NULL,也就是链表的结尾
stud *ptr1=head;
stud *ptr2=head->link;

while(ptr1->link!=temp)//(ptr1!=NULL)
{
//ptr2=ptr1->link; //放在循环体下面了
while(ptr2->link!=temp)//(ptr2!=NULL)
{
if(ptr1->link->score > ptr2->link->score) //(ptr1->score > ptr2->score)
{//交换 ptr1->link和ptr2->link,而不是ptr1和ptr2,否则无法交换
ptr1->link=ptr2->link;
ptr2->link=ptr2->link->link;//temp->link=ptr2;
ptr1->link->link=ptr2;//ptr2->link=ptr1;
}
ptr1=ptr1->link;//ptr2=ptr2->link;
ptr2=ptr1->link;//从上面移动下来的
}
temp=ptr2;//新加的
ptr1=head;//ptr1=ptr1->link;
ptr2=ptr1->link;//从上面移动下来的
}
return (head);
}

stud * creat(int n)
{
stud *p,*h,*s;
int i;

if((h=(stud *)malloc(sizeof(stud)))==NULL)
{
printf("不能分配内存空间!");
exit(0);
}
h->name[0]='\0';
h->link=NULL;
p=h;
for(i=0;i<n;i++)
{
if((s= (stud *) malloc(sizeof(stud)))==NULL)
{
printf("不能分配内存空间!");
exit(0);
}
s->link=NULL;//p->link=s; //跟下句对调了一下,为了把相关的代码放在一起
printf("请输入第%d个人的姓名",i+1);
scanf("%s",s->name);
printf("请输入第%d个人的分数",i+1);
scanf("%d",&s->score);
p->link=s; //s->link=NULL;//跟上句对调了一下,为了把相关的代码放在一起
p=s;
}
return(h);
}

void print(stud *h)
{
stud *p;

p=h->link;
printf("数据信息为:\n");
while(p!=NULL)
{
printf("%s ",&*(p->name));
printf("的分数为%d\n",p->score);
p=p->link;
}
}

void main()
{
stud *head;
head=creat(N);
head=sort(head);
print(head);
getchar();
}

Ⅳ c语言数据结构(双向链表排序)

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
float data;
struct node *pre;
struct node *next;
}*DoubleNode;

void sort_DoubleNode(DoubleNode &head, float data)
{
DoubleNode node = (DoubleNode)malloc(sizeof(struct node));
DoubleNode p,q;
node->data = data;
if(head == NULL)
{
head = node;
head->next = NULL;
head->pre = NULL;
return;
}

p = head;
while(p != NULL && p->data < data)
{
q = p;
p = p->next;
}
if(p == head)
{
node->next = head;
node->pre = NULL;
head ->pre = node;
head = node;
}
else{
q->next = node;
node->pre = q;
node->next = p;
if(p != NULL) p->pre = node;
}
}

void out_DoubleNode(DoubleNode &head)
{
DoubleNode p = head;
while(p != NULL)
{
printf("%.4f ", p->data);
p = p->next;
}
printf("\n");
}

void main()
{
int n;
float data;
DoubleNode head = NULL;
scanf("%d", &n);

for(int i=0; i<n; ++i)
{
scanf("%f", &data);
sort_DoubleNode(head, data);
}

out_DoubleNode(head);
}
/*
15
23.4 4.5 56 67 7.5 90 8.97 90.6 25.6 145
856 10.2 0.36 54.6 56.1
*/

第二题: 测试数据同第一题
#include <stdio.h>
#include <stdlib.h>

typedef struct node{
float data;
struct node *pre;
struct node *next;
}*DoubleNode;

bool bigger(float a, float b)
{
return (a > b);
}

bool smaller(float a, float b)
{
return (a < b);
}

void sort_DoubleNode(DoubleNode &head, float data, bool (*comp)(float a, float b))
{
DoubleNode node = (DoubleNode)malloc(sizeof(struct node));
DoubleNode p,q;
node->data = data;
if(head == NULL)
{
head = node;
head->next = NULL;
head->pre = NULL;
return;
}

p = head;
while(p != NULL && comp(p->data, data))
{
q = p;
p = p->next;
}
if(p == head)
{
node->next = head;
node->pre = NULL;
head ->pre = node;
head = node;
}
else{
q->next = node;
node->pre = q;
node->next = p;
if(p != NULL) p->pre = node;
}
}

void merge_DoubleNode(DoubleNode &head, DoubleNode &head1, DoubleNode &head2)
{
DoubleNode p = head2;
head = head1;
if(head1 == NULL || head2 == NULL) return;
while((head1->next != NULL) && (head2 != NULL))
{
p = head2->next;
head1->next->pre = head2;
head2->next = head1->next;
head2->pre = head1;
head1->next = head2;
head1 = head2->next;
head2 = p;
}
if((head1->next == NULL) && (head2 != NULL))
{
head1->next = head2;
head2->pre = head1;
}
}

void out_DoubleNode(DoubleNode &head)
{
DoubleNode p = head;
while(p != NULL)
{
printf("%.4f ", p->data);
p = p->next;
}
printf("\n");
}

void main()
{
int n;
float data;
DoubleNode head = NULL;
DoubleNode head1 = NULL;
DoubleNode head2 = NULL;
scanf("%d", &n);

for(int i=0; i<n; ++i)
{
scanf("%f", &data);
if((i+1)%2 == 0) sort_DoubleNode(head2, data, &bigger);
else sort_DoubleNode(head1, data, &smaller);
}

merge_DoubleNode(head, head1, head2);

out_DoubleNode(head);
}
/*
15
23.4 4.5 56 67 7.5 90 8.97 90.6 25.6 145
856 10.2 0.36 54.6 56.1
*/

Ⅳ C语言链表冒泡排序

我这个效率要高一些,呵呵。

#include <stdio.h>

typedef struct listnode
{
int f;
struct listnode *next;
} ListNode;

ListNode *sort(ListNode *head)
{
ListNode *p,*p1,*p2,*p3;
ListNode h, t;
if (head == NULL) return NULL;
h.next=head;
p=&h;
while (p->next!=NULL)
{
p=p->next;
}
p=p->next=&t;
while (p!=h.next)
{
p3=&h;
p1=p3->next;
p2=p1->next;
while (p2!=p)
{
if ((p1->f)>(p2->f))
{
p1->next=p2->next;
p2->next=p1;
p3->next=p2;

p3=p2;
p2=p1->next;

} else {
p3=p1;
p1=p2;
p2=p2->next;
}
}
p=p1;
}
while (p->next!=&t)
{
p=p->next;
}
p->next=NULL;
return h.next;
}

int main() {
ListNode h,j,k,l;
h.next=&j;
h.f=3;
j.next=&k;
j.f=5;
k.next=&l;
k.f=1;
l.next=NULL;
l.f=7;
ListNode* p = sort(&h);
while (p != NULL) {
printf("%d ", p->f);
p=p->next;
}
printf("\n");
return 0;
}

Ⅵ C语言,链表怎么从大到小排序

//创建data型结构,为生成链表做准备
struct data
{
int value;
data *next;
};

//对链表进行排列的函数
void line(data *p,int n)
{ int temp,i,j;
data *tp;
for(i=0;i<n-1;i++)
for(j=0,tp=p;j<n-i-1;j++,tp=tp->next)
{ if(tp->value>tp->next->value)
{temp=tp->next->value;
tp->next->value=tp->value;
tp->value=temp;
}
}
}
以上是冒泡法对链表排专序的例子;
//输出答案的属函数
void answer(data *p)
{ while(p!=NULL) {
cout<<p->value<<endl;
p=p->next;

}
}
以上是遍历链表的函数p是链表的头指针

Ⅶ C语言链表排序

#include"stdafx.h"

#include<stdlib.h>

//创建一个节点,data为value,指向NULL

Node*Create(intvalue){

Node*head=(Node*)malloc(sizeof(Node));

head->data=value;

head->next=NULL;

returnhead;

//销毁链表

boolDestroy_List(Node*head){

Node*temp;

while(head){

temp=head->next;

free(head);

head=temp;

head=NULL;

returntrue;

//表后添加一个节点,Create(value)

boolAppend(Node*head,intvalue){

Node*n=Create(value);

Node*temp=head;

while(temp->next){

temp=temp->next;

temp->next=n;

return0;

//打印链表

voidPrint_List(Node*head){

Node*temp=head->next;

while(temp){

printf("%d->",temp->data);

temp=temp->next;

printf("\n");

//在链表的第locate个节点后(头节点为0)插入创建的节点Create(value)

boolInsert_List(Node*head,intlocate,intvalue){

Node*temp=head;

Node*p;

Node*n=Create(value);

if(locate<0)

returnfalse;

while(locate--){

if(temp->next==NULL){

temp->next=Create(value);

returntrue;

temp=temp->next;

p=temp->next;

temp->next=n;

n->next=p;

returntrue;

//删除第locate个节点后(头节点为0)的节点

boolDelete_List(Node*head,intlocate){

Node*temp=head;

Node*p;

if(locate<0)

returnfalse;

while(locate--){

if(temp==NULL){

returnfalse;

temp=temp->next;

p=temp->next->next;

free(temp->next);

temp->next=NULL;

temp->next=p;

returntrue;

//获取链表长度(不包括头节点)

intSize_List(Node*head){

Node*temp=head;

intsize=0;

while(temp->next){

temp=temp->next;

size++;

returnsize;

//链表的三种排序(选择,插入,冒泡)

boolSort_List(Node*head){

intt=0;

intsize=Size_List(head);

//选择排序

/*for(Node*temp=head->next;temp!=NULL;temp=temp->next){

for(Node*p=temp;p!=NULL;p=p->next){

if(temp->data>p->data){

printf("换%d和%d\n",temp->data,p->data);

t=temp->data;

temp->data=p->data;

p->data=t;

}*/

//插入排序

/*for(Node*temp=head->next->next;temp!=NULL;temp=temp->next){

for(Node*p=head;p->next!=NULL;p=p->next){

if(p->next->data>temp->data)

printf("换%d和%d\n",temp->data,p->next->data);

t=temp->data;

temp->data=p->next->data;

p->next->data=t;

}*/

//冒泡排序

for(Node*temp=head->next;temp->next!=NULL;temp=temp->next){

for(Node*p=head->next;p->next!=NULL;p=p->next){

if(p->data>p->next->data){

t=p->data;

p->data=p->next->data;

p->next->data=t;

return0;

(7)c语言链表的排序扩展阅读:

return表示把程序流程从被调函数转向主调函数并把表达式的值带回主调函数,实现函数值的返回,返回时可附带一个返回值,由return后面的参数指定。

return通常是必要的,因为函数调用的时候计算结果通常是通过返回值带出的。如果函数执行不需要返回计算结果,也经常需要返回一个状态码来表示函数执行的顺利与否(-1和0就是最常用的状态码),主调函数可以通过返回值判断被调函数的执行情况。

Ⅷ C语言中,创建的链表怎么排序!

先创建一个结构体,内容包括编号,姓名,
然后,再对姓名进行排序
,排序方法有选择法,冒泡法,等都可以,
最后再输出

Ⅸ C语言链表中如何实现对一组数据进行排序

#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct student * creat();
struct student * link(struct student * head_a,struct student * head_b);
void print(struct student * head);
struct student{
int num;
float score[2];
struct student *next;
}stu;
int main(void)
{
struct student *head_a;
struct student *head_b,*head_c;
printf("请输入a链表学生的数据:0 0 0结束输入\n");
head_a=creat();
print(head_a);
printf("请输入b链表学生的数据:0 0 0结束输入\n");
head_b=creat();
print(head_b);
head_c=link(head_a,head_b);
printf("打印经过排序之后的学生数据,a,b链数据的结合\n");
print(head_c);
return 0;
}

struct student * creat()
{
int n=0;
struct student * head,*p1,*p2;
p1=p2=(struct student *)malloc(sizeof(struct student ));
scanf("%d%f%f",&p1->num,&p1->score[0],&p1->score[1]);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
scanf("%d%f%f",&p1->num,&p1->score[0],&p1->score[1]);
}
p2->next=NULL;
return head;
}

void print(struct student * head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%-10d%-10.1f%-10.1f\n",p->num,p->score[0],p->score[1]);
p=p->next;
}
return ;

}

struct student * link(struct student * head_a,struct student * head_b)
{
int n,m;
m=n=0;
struct student * head,*p1,*p2,*p3,*q;//q是在冒泡排序是(共需N-1趟排序)每趟的最后一次指针p1的位置,开始时q为Null
p1=head_a;
p2=head_b;
head=head_a;

while(p1->next!=NULL)
{p1=p1->next;n++;}
p1->next=p2;
p1=head_a;
while(p1!=NULL)
{
p1=p1->next;
n++; //n是计算链表的节点数,以备后面的排序用
}
q=NULL;
p1=head_a;
p2=p1->next ;
while(m<n)
{
m++;
//以下是采用冒泡法进行排序
while(p2!=q)
{
if(p1->num>p2->num)
{
if(head==p1) head=p2;
else p3->next=p2;
p1->next=p2->next;p2->next=p1;
//以下是按照 p3 p1 p2排序
p3=p2;p2=p1->next;
}
else
{
p3=p1;p1=p1->next;p2=p2->next;
}
}
q=p1;p1=head;p2=p1->next;
}
return (head);
}