数据结构链表冒泡排序

7 篇文章 0 订阅
3 篇文章 0 订阅

链表冒泡排序

前序

	6000字文章,建议先自上而下的划分函数,读者先自己实现一下功能,
   	然后再看该文章才会目标明确,有方向感。
   	这里采用链表冒泡排序,需要有二级指针和链表的基础。

源程序

/*	单链表 排序 
@author:	赵雨腾
@data:		12th	March	2020 
@input:	 49 38 65 97 76 13 27 49 -1
@output:	 The new list is:13 27 38 49 49 65 76 97
*/
#include<stdio.h>
#include<stdlib.h>
#define ElementType int
struct listNode{
	 ElementType data;
	struct listNode * nextPtr; 
};
typedef struct listNode LISTNODE;
typedef LISTNODE * LISTNODEPTR;

int getSize (LISTNODEPTR currentPtr);//长度 
void bubbleSort(LISTNODEPTR headPtr,int len);//冒泡 
void createListHead(LISTNODEPTR * headPtrPtr);//创建空头 
void insertEnd2(LISTNODEPTR *lastPtrPtr, ElementType value);//尾插2 
void destroyList(LISTNODEPTR headPtr);//free 
void printfList(LISTNODEPTR currentPtr);//打印 

int main(void)
{
	LISTNODEPTR headPtr,lastPtr,currentPtr;
	createListHead(&headPtr);//创建空头 
	lastPtr = headPtr;//尾指针就是空头 
	
	int num,len; 
	scanf("%d",&num);
	
	 //链表创建 
	while(num!=-1){
		insertEnd2(&lastPtr,num);
		scanf("%d",&num);
	}
	
	len = getSize(headPtr); //长度 
	bubbleSort(headPtr,len); //冒泡 

	printf("The new list is:") ;
	printfList(headPtr);//打印 		
		
	destroyList(headPtr);//free 
} 
//长度 
int getSize (LISTNODEPTR currentPtr){
	int num = -1;/*-1的初始值是为了抵消空节点的计数*/
	while(currentPtr!=NULL){
		num++;
		currentPtr=currentPtr->nextPtr;
     } 
	return (num);
}
//冒泡	排序 
void bubbleSort(LISTNODEPTR headPtr,int len)
{
	LISTNODEPTR previousPtr,currentPtr,backPtr,tempPtr;
	int i,j;
	
	for(i=len-1; i>=1 ;i--)
	{
		previousPtr						=headPtr;
		currentPtr						=previousPtr->nextPtr;
		backPtr							=currentPtr->nextPtr;	
//		tempPtr							=backPtr->nextPtr;
		
		for(j=0; j<=i-1 ;j++)
		{
			if(currentPtr->data > backPtr->data)
			{
				tempPtr						=backPtr->nextPtr;
				//交换  current  back  指针域 
				previousPtr->nextPtr		=backPtr;
				backPtr->nextPtr			=currentPtr;
				currentPtr->nextPtr			=tempPtr; 
				//p b c	修正 
				previousPtr=backPtr;
				currentPtr=currentPtr;
				backPtr=currentPtr->nextPtr;			
			}
			else
			{
				//p c b	修正 
				previousPtr					=currentPtr;
				currentPtr					=backPtr;
				backPtr						=backPtr->nextPtr;				
			}

		}

	} 
} 
//空头 
void createListHead(LISTNODEPTR * headPtrPtr){
	*headPtrPtr = (LISTNODEPTR)malloc(sizeof(LISTNODE));
	(*headPtrPtr)->nextPtr=NULL;
}
//尾插 
void insertEnd2(LISTNODEPTR *lastPtrPtr, ElementType value){
	LISTNODEPTR newPtr;
	newPtr = (LISTNODEPTR)malloc(sizeof(LISTNODE));
	if(newPtr!=NULL){
		//	上膛 
		newPtr->data		=value;
		newPtr->nextPtr		=NULL;
		// 	链接 
		(*lastPtrPtr)->nextPtr=newPtr;
		//修正 
		*lastPtrPtr			=newPtr;
	}
} 
//free 
void destroyList(LISTNODEPTR headPtr){
	LISTNODEPTR tempPtr;
	while(headPtr!=NULL){
		tempPtr			=headPtr;
		headPtr			=headPtr->nextPtr;
		free(tempPtr);
	}
} 
//打印 
void printfList( LISTNODEPTR currentPtr)
{
	//跳过	空节点 
	currentPtr = currentPtr->nextPtr;
	if(currentPtr!=NULL)
	{
		//打印 
		while(currentPtr!=NULL){
			printf("%d ",currentPtr->data);
			currentPtr = currentPtr->nextPtr;
		}
	}
} 

讲解

头文件
#include<stdio.h>
#include<stdlib.h>
结构体
#define ElementType int

struct listNode{
	 ElementType data;
	struct listNode * nextPtr; 
};
typedef struct listNode LISTNODE;
typedef LISTNODE * LISTNODEPTR;

// LISTNODE 定义结构体
// LISTNODEPTR 定义结构体指针

主函数
int main(void)
{
	LISTNODEPTR headPtr,lastPtr,currentPtr;
	createListHead(&headPtr);//创建空头 
	lastPtr=headPtr;//尾指针就是空头 
	
	int num,len; 
	scanf("%d",&num);
	
	 //链表创建 
	while(num!=-1){
		insertEnd2(&lastPtr,num);
		scanf("%d",&num);
	}
	
	len = getSize(headPtr); //长度 
	bubbleSort(headPtr,len); //冒泡 

	printf("The new list is:") ;
	printfList(headPtr);//打印 		
		
	destroyList(headPtr);//free 
} 
void createListHead(LISTNODEPTR * headPtrPtr);//创建空头

// 函数接口:LISTNODEPTR * 是二级指针
// 函数调用:createListHead(&headPtr);//创建空头
// 函数定义如下:

//空头 
void createListHead(LISTNODEPTR * headPtrPtr){
	*headPtrPtr=(LISTNODEPTR)malloc(sizeof(LISTNODE));
	(*headPtrPtr)->nextPtr=NULL;
}
void insertEnd2(LISTNODEPTR *lastPtrPtr, ElementType value);//尾插2

//函数接口:
// LISTNODEPTR *是二级指针
// ElementType value 待排序的元素,这里是int类型
// 函数调用:insertEnd2(&lastPtr,num);//尾插创建列表
// 函数定义如下:

//尾插 
void insertEnd2(LISTNODEPTR *lastPtrPtr, ElementType value){
	LISTNODEPTR newPtr;
	newPtr = (LISTNODEPTR)malloc(sizeof(LISTNODE));
	if(newPtr!=NULL){
		//	上膛 
		newPtr->data		=value;
		newPtr->nextPtr		=NULL;
		// 	链接 
		(*lastPtrPtr)->nextPtr=newPtr;
		//修正 
		*lastPtrPtr			=newPtr;
	}
} 

// 该函数在主函数中的调用如下:

	LISTNODEPTR headPtr,lastPtr,currentPtr;
	createListHead(&headPtr);//创建空头 
	lastPtr=headPtr;//尾指针就是空头 
	
	int num,len; 
	scanf("%d",&num);
	
	 //链表创建 
	while(num!=-1){
		insertEnd2(&lastPtr,num);
		scanf("%d",&num);
	}
int getSize (LISTNODEPTR currentPtr);//长度

// 函数接口: LISTNODEPTR 一级指针
// 函数调用: len=getSize(headPtr); //长度
// 函数定义如下:

//长度 
int getSize (LISTNODEPTR currentPtr){
	int num=-1;/*-1的初始值是为了抵消空节点的计数*/
	while(currentPtr!=NULL){
		num++;
		currentPtr=currentPtr->nextPtr;
     } 
	return (num);
}
void bubbleSort(LISTNODEPTR headPtr,int len);//冒泡

// 函数接口: LISTNODEPTR 一级指针
// 函数调用: bubbleSort(headPtr,len); //冒泡
// 函数定义如下:

//冒泡	排序 
void bubbleSort(LISTNODEPTR headPtr,int len)
{
	LISTNODEPTR previousPtr,currentPtr,backPtr,tempPtr;
	int i,j;
	
	for(i=len-1;i>=1;i--)
	{
		previousPtr						=headPtr;
		currentPtr						=previousPtr->nextPtr;
		backPtr							=currentPtr->nextPtr;	
//		tempPtr							=backPtr->nextPtr;
		
		for(j=0;j<=i-1;j++)
		{
			if(currentPtr->data > backPtr->data)
			{
				tempPtr						=backPtr->nextPtr;
				//交换  current  back  指针域 
				previousPtr->nextPtr		=backPtr;
				backPtr->nextPtr			=currentPtr;
				currentPtr->nextPtr			=tempPtr; 
				//p b c	修正 
				previousPtr=backPtr;
				currentPtr=currentPtr;
				backPtr=currentPtr->nextPtr;			
			}
			else
			{
				//p c b	修正 
				previousPtr					=currentPtr;
				currentPtr					=backPtr;
				backPtr						=backPtr->nextPtr;				
			}

		}

	} 
} 

// 该函数在主函数中的调用如下:

	len = getSize(headPtr); //长度 
	bubbleSort(headPtr,len); //冒泡 
void printfList(LISTNODEPTR currentPtr);//打印

// 函数接口:LISTNODEPTR 一级指针
// 函数调用: printfList(headPtr);//打印
// 函数定义如下:

//打印 
void printfList( LISTNODEPTR currentPtr)
{
	//跳过	空节点 
	currentPtr=currentPtr->nextPtr;
	if(currentPtr!=NULL)
	{
		//打印 
		while(currentPtr!=NULL){
			printf("%d ",currentPtr->data);
			currentPtr=currentPtr->nextPtr;
		}
	}
} 

// 该函数在主函数中的调用如下:

	printf("The new list is:") ;
	printfList(headPtr);//打印 	
void destroyList(LISTNODEPTR headPtr);//free

// 函数接口:LISTNODEPTR 一级指针
// 函数调用: destroyList(headPtr);//free
// 函数定义如下:

//free 
void destroyList(LISTNODEPTR headPtr){
	LISTNODEPTR tempPtr;
	while(headPtr!=NULL){
		tempPtr			=headPtr;
		headPtr			=headPtr->nextPtr;
		free(tempPtr);
	}
} 

// 该函数在主函数中的调用如下:

	destroyList(headPtr);//free 

结束语

	不妥之处,还请读者斧正~
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值