C语言做链表的排序

Python012

C语言做链表的排序,第1张

#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;

扩展资料:

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

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

同学,给你一段代码,里面涵盖了链表的冒泡排序!

#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=SLp!=NULLp=p->next)

{

for(q=p->nextq!=NULLq=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=LS!=NULLS=S->next)

printf("%d ",S->data)

Sort(L)

printf("\n按递增顺序排序后的序列为:\n")

for(K=LK!=NULLK=K->next)

printf("%d==>",K->data)

return 0

}

这个是我曾经编写的一个管理里面的一个模块

就是按链表的某个变量进行排序的

希望您能看明白

Ranking_inquires(struct

student

*head)

{

int

B=A,i=0

struct

student

*temp=head

struct

student

*p=head

struct

student

*q=head

printf("\t\t

按总分

名次查询

\n

")

printf("姓名

总分

名次\n")

while(p->next!=NULL&&A>1)//根据成员的成绩对结构体

进行排序

{

p->sum=p->math+p->English+p->chinese+p->computer

q=p->next

p=p->next

while(q->next!=NULL)

{

q=q->next

if(p->sum

sum)

{

h.stu_id1=p->stu_id

strcpy(h.name1,p->name)

h.English1=p->English

h.computer1=p->computer

h.math1=p->math

h.sum1=p->sum

h.chinese1=p->chinese

p->stu_id=q->stu_id

strcpy(p->name,q->name)

p->English=q->English

p->computer=q->computer

p->math=q->math

p->sum=q->sum

p->chinese=q->chinese

q->stu_id=h.stu_id1

strcpy(q->name,h.name1)

q->English=h.English1

q->computer=h.computer1

q->math=h.math1

q->sum=h.sum1

q->chinese=h.chinese1

}

}

A--

++i

p->ranking=i

}

这个是定义的全局变量

int

A=0

int

cc

struct

student

{

int

stu_id

char

name[20]

float

English

float

computer

float

chinese

float

math

float

sum

int

ranking

struct

student

*next

}

struct

stu

{

int

stu_id1

char

name1[20]

float

English1

float

computer1

float

chinese1

float

math1

float

sum1

}h