当前位置:首页 » 编程语言 » c语言将无序的链表排序
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言将无序的链表排序

发布时间: 2022-05-30 06:39:30

‘壹’ 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语言的链表怎么排序

其实最简单的方法就是,重新建一个链表存储有序序列,把原链表里的元素一个一个地取出来,放到新链表里。放进去的时候二分一下当前序列(可能要比二分数组慢一点,因为要一个一个地往前或者往后找,没有直接按下标找方便),找到当前元素在序列里正确的位置。这种方法的时间复杂度大概在O(n*log2n)左右
基本流程:
初始状态:
原链表:1--->2--->4--->3--->NULL
新链表:NULL
第一次处理:
原链表:2--->4--->3--->NULL
新链表:1--->NULL
第二次处理:
原链表:4--->3--->NULL
新链表:1--->2--->NULL
第三次处理:
原链表:3--->NULL
新链表:1--->2--->4--->NULL
第四次处理:
原链表:NULL
新链表:1--->2--->3--->4--->NULL
或者建一个二叉树,类似于bst的结构(左子<根<右子),再中序遍历一下。
或者不用那些基于比较的排序,用基数排序一类的方法,可以在O(n)的时间内解决问题C+_+C

‘叁’ 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语言链表排序

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

(4)c语言将无序的链表排序扩展阅读:

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

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

‘伍’ C语言 链表怎么排序 急求大虾

排序!这是一个庞大的话题,有插入排序,插入排序又分直接插入排序、希尔排序等,还有交换排序,交换排序有冒泡排序、快速排序,还有选择排序,有直接选择排序、归并排序等等…而且还不断的有新的排序方法产生…不知道你要哪一种…新手一般用选择排序和冒泡排序,方法简单,两重循环。

‘陆’ C语言单向链表排序如何实现

struct student* printf_sort(struct student *head)
{
struct student *p1,*p2,*ptemp,*pfinished=NULL;
for(p1=head;p1->next!=pfinished;)//对链表进行从大到小排序(这里用冒泡法)
//p1使之总是指向头结点,pfinished使之总是指向已排序好的最前面的结点
//ptemp作为中介,保存p2的上一个结点
{
for(p2=p1;p2->next!=pfinished;)
{
if(p2->num<p2->next->num)//p2的值小于p2->next的值,交换 {
if(p2==p1)//头结点要交换
{
p1=p2->next;
p2->next=p1->next;
p1->next=p2;
ptemp=p1;
}
else
{
ptemp->next=p2->next;
ptemp=p2->next;
p2->next=ptemp->next;
ptemp->next=p2;
}
}
else//不需要交换,则p2、ptemp前进1位
{
ptemp=p2;
p2=p2->next;
}
}
pfinished=p2;
}
}

‘柒’ 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语言 单向链表如何排序

void link_order(STU *p_head)

{

STU *pb, *pf, temp;

pf = p_head;

if(p_head == NULL) {//链表为空

printf("needn't order. ");

return ;

}

if(p_head->next == NULL) {//链表有1个节点

printf("only one print, needn't order. ");

return ;

}

while(pf->next != NULL) {//以pf指向的节点为基准节点

pb = pf->next;//pb从基准点的下一个节点开始

while(pb != NULL) {

if(pf->num > pb->num) {

temp = *pf;

*pf = *pb;

*pb = temp;

temp.next = pf->next;

pf->next = pb->next;

pb->next = temp.next;

}

pb = pb->next;

}

pf = pf->next;

}

return ;

}

(8)c语言将无序的链表排序扩展阅读:

链表的排序有三种情况:

1、链表为空时:不用排序;

2、链表中有一个节点:不用排序;

3、链表中两个及其以上节点时:排序。

return 0代表程序正常退出。return是C++预定义的语句,它提供了终止函数执行的一种方式。当return语句提供了一个值时,这个值就成为函数的返回值。

return语句用来结束循环,或返回一个函数的值。

1、return 0,说明程序正常退出,返回到主程序继续往下执行。

2、return 1,说明程序异常退出,返回主调函数来处理,继续往下执行。return 0或return 1对程序执行的顺序没有影响,只是大家习惯于使用return(0)退出子程序而已。

‘玖’ 一道C语言中关于链表排序的代码的思路与步骤

/*代码的整体思路是分为3个函数,一个建立链表,一个链表排序,最后一个是输出链表,
而程序调用也是根据这个顺序来调用的,详细说明在下面*/
#include
#include
#define
LEN
sizeof(struct
number)
struct
number
{
int
num;
struct
number
*next;
};
int
n=0,sum=0;
void
main()
{
struct
number
*creat();
void
print(struct
number
*head);
struct
number
*change(struct
number
*head);
struct
number
*ahead;
ahead=creat();
sum+=n;
//sum计算结点的总数
change(ahead);
print(ahead);
}
struct
number
*creat()
//创建链表
{
struct
number
*p1,*p2,*head;
p1=p2=(struct
number*)malloc(LEN);
//LEN就是define定义的sizeof(struct
number)
printf("input
a
number,not
zero\n");
scanf("%d",&p1->num);
//将p1的num赋值为输入的数
while(p1->num!=0)
//如果输入的数不是0
{
n++;
//是记录链表节点的数量
if(n==1)
//如果n是建立的第一个结点,也就是链表的头
head=p1;
else
//否则p2的next指针指向p1,也就是原来链表最后一个结点的next指向新结点p1
p2->next=p1;
p2=p1;
//p2指向p1,每次p2都指向链表的结尾
p1=(struct
number*)malloc(LEN);
//申请内存,建立新结点
scanf("%d",&p1->num);
//再输入号
}
p2->next
=NULL;
//将尾结点的next指针指向NULL
return
head;
//返回头指针
}
struct
number
*change(struct
number
*head)
{
struct
number
*p,*h;
int
i;
for(i=1;i
next;p->next!=NULL;p=p->next,h=h->next)
//p指向头结点,h指向p的后一个结点,每循环一次p和h都往后指一个结点
if(p->num>h->num)
//如果前面的结点比后面结点大,就交换,只交换num的值
{
int
t;
t=p->num;
p->num=h->num;
h->num=t;
}
return
head;
}
void
print(struct
number
*head)
//链表的便利,只便利num值
{
struct
number
*p;
p=head;
while(p!=NULL)
{
printf("%d",p->num
);
p=p->next
;
}
}

‘拾’ 关于C语言链表排序的问题

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

structnumber//链表节点
{
intnum;
structnumber*next;
};

intn;

structnumber*creat()//创建链表
{
structnumber*head;//头节点
structnumber*p1,*p2;
n=0;
p1=p2=(structnumber*)malloc(sizeof(structnumber));
scanf("%d",&p1->num);
head=NULL;
while(p1->num!=0)//循环输入链表数据,当输入为0时结束,链表数据不包括0
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(structnumber*)malloc(sizeof(structnumber));
scanf("%d",&p1->num);
}
p2->next=NULL;
return(head);
}

voidprint(structnumber*head)//链表从小到大排序,并输出
{
structnumber*p1,*p2,*p;
inti,j,t;
printf("这%d个数从小到大的排序为: ",n);
if(head!=NULL)
{
//冒泡排序
for(j=0;j<n-1;j++)
{
p1=head;p2=head;
for(i=0;i<n-1-j;i++)
{
p2=p1->next;
if(p1->num>=p2->num)
{
t=p1->num;
p1->num=p2->num;
p2->num=t;
}
p1=p1->next;
}
}
}
p=head;
if(head!=NULL)
{
//输出链表值
do
{
printf("%3d",p->num);
p=p->next;
}while(p!=NULL);
}
}

voidmain()
{
structnumber*head;
head=creat();
print(head);
}