链表插入排序 -- C语言

83 篇文章 2 订阅
30 篇文章 0 订阅

这里使用插入排序法,因为插入排序法涉及有序区域的后移,所以比较适合使用链表进行做。因为单链表没有prev的默认指针,所以不方便使用快速排序法。综合选择了插入排序

算法草稿

代码实现

int insertSortList(st_dataNode** phead){
	if(NULL == phead || NULL == *phead){
		printf("%s: param error\n",__func__);
		return PARAM_ERR;
	}	

	st_dataNode * head = NULL;
	st_dataNode * prev = NULL, * cur = NULL, * next = NULL;  	/*遍历外循环使用*/
	st_dataNode * inprev = NULL, * incur = NULL, *innext = NULL, *intail = NULL;	/*遍历有序区使用*/

	head = *phead;
	cur = head->next; /*第一个元素默认是有序区,默认不排序*/
	prev = head;
	if(NULL == cur){ /* 就一个元素,排个毛序 */
		return SUCCESS;
	} 

	/*遍历无序区*/
	while(NULL != cur){
		
		next = cur->next;
#ifdef DEBUG
		if(NULL != next){
			printf("无序区 \n\t prev->data = %d, cur->data = %d, next->data = %d, head->data = %d \n", \
				prev->data, cur->data, next->data, head->data);
		}
		dumpList(head);
#endif
		/*
		 * 链表头处理
		 * 最小的元素插入到链表头
		 */
		if(cur->data < head->data){
#ifdef DEBUG
			printf("List Head \n\tprev->data = %d, cur->data = %d, next->data = %d, head->data = %d \n", \
				prev->data, cur->data, next->data, head->data);
			dumpList(head);
#endif		
			/* 摘掉 cur */
			prev->next = next;
			/* 插入到头 */
			cur->next = head;
			/* 更新头 */
			head = cur;
			
			/*更新cur, 因为摘除掉了cur, prev应该不变*/			
			cur = next;
#ifdef DEBUG
			dumpList(head);
#endif
			continue;
		}

		/*
		 * 中间元素
		 * 在有序区[head, pos, ...., cur-1]里面查找合适的插入位置
		 */
		inprev = head;
		incur = head->next;
		innext = incur->next;
		intail = cur;
#ifdef DEBUG
		printf("inprev->data = %d, incur->data = %d, innext->data = %d, intail->data = %d, cur->data = %d \n", \
			inprev->data, incur->data, innext->data, intail->data, cur->data);
#endif
		while(incur != intail){
			innext = incur->next;			
#ifdef DEBUG
			printf("Inner Loop \n");
			if(NULL != next){
				printf("\t inprev->data = %d, incur->data = %d, innext->data = %d, intail->data = %d, cur->data = %d, prev->data = %d, next->data = %d \n", \
					inprev->data, incur->data, innext->data, intail->data, cur->data, prev->data, next->data);
			}

			dumpList(head);
#endif

			if(cur->data < incur->data){
#ifdef DEBUG
				printf("Before insert mid nodes \n");
				if(NULL != next){
					printf("\t inprev->data = %d, incur->data = %d, innext->data = %d, intail->data = %d, cur->data = %d, prev->data = %d, next->data = %d \n", \
						inprev->data, incur->data, innext->data, intail->data, cur->data, prev->data, next->data);
				}

				dumpList(head);
#endif				
				/*摘掉cur*/
				prev->next = next;
			
				/*插入合适位置*/
				inprev->next = cur;
				cur->next = incur;	
#ifdef DEBUG
				printf("After insert mid nodes \n");
				dumpList(head);
#endif
				/*更新cur*/
				goto cont;
			}	

	
			inprev = incur;
			incur = innext;
		}

		/*移动到下一个*/
		prev = cur;	
cont:		
		cur = next;	
		
	}

	*phead = head;

	return SUCCESS;

}


void testInsertSortList(void){
	printf("====== %s ===========\n", __func__);
	insertSortList(&ghead);
	dumpList(ghead);

	return;
}

调试编译

gcc listMain.c list.c -o a.exe -DDEBUG

调试输出


====== testInsertSortList ===========
无序区
         prev->data = 22, cur->data = 32, next->data = 19, head->data = 22
========= Dump List 0x1661010 ===========
         22  32  19  53  0  47  29  116  4  6
===================================
inprev->data = 22, incur->data = 32, innext->data = 19, intail->data = 32, cur->data = 32
无序区
         prev->data = 32, cur->data = 19, next->data = 53, head->data = 22
========= Dump List 0x1661010 ===========
         22  32  19  53  0  47  29  116  4  6
===================================
List Head
        prev->data = 32, cur->data = 19, next->data = 53, head->data = 22
========= Dump List 0x1661010 ===========
         22  32  19  53  0  47  29  116  4  6
===================================
========= Dump List 0x1661050 ===========
         19  22  32  53  0  47  29  116  4  6
===================================
无序区
         prev->data = 32, cur->data = 53, next->data = 0, head->data = 19
========= Dump List 0x1661050 ===========
         19  22  32  53  0  47  29  116  4  6
===================================
inprev->data = 19, incur->data = 22, innext->data = 32, intail->data = 53, cur->data = 53
Inner Loop
         inprev->data = 19, incur->data = 22, innext->data = 32, intail->data = 53, cur->data = 53, prev->data = 32, next->data = 0
========= Dump List 0x1661050 ===========
         19  22  32  53  0  47  29  116  4  6
===================================
Inner Loop
         inprev->data = 22, incur->data = 32, innext->data = 53, intail->data = 53, cur->data = 53, prev->data = 32, next->data = 0
========= Dump List 0x1661050 ===========
         19  22  32  53  0  47  29  116  4  6
===================================
无序区
         prev->data = 53, cur->data = 0, next->data = 47, head->data = 19
========= Dump List 0x1661050 ===========
         19  22  32  53  0  47  29  116  4  6
===================================
List Head
        prev->data = 53, cur->data = 0, next->data = 47, head->data = 19
========= Dump List 0x1661050 ===========
         19  22  32  53  0  47  29  116  4  6
===================================
========= Dump List 0x1661090 ===========
         0  19  22  32  53  47  29  116  4  6
===================================
无序区
         prev->data = 53, cur->data = 47, next->data = 29, head->data = 0
========= Dump List 0x1661090 ===========
         0  19  22  32  53  47  29  116  4  6
===================================
inprev->data = 0, incur->data = 19, innext->data = 22, intail->data = 47, cur->data = 47
Inner Loop
         inprev->data = 0, incur->data = 19, innext->data = 22, intail->data = 47, cur->data = 47, prev->data = 53, next->data = 29
========= Dump List 0x1661090 ===========
         0  19  22  32  53  47  29  116  4  6
===================================
Inner Loop
         inprev->data = 19, incur->data = 22, innext->data = 32, intail->data = 47, cur->data = 47, prev->data = 53, next->data = 29
========= Dump List 0x1661090 ===========
         0  19  22  32  53  47  29  116  4  6
===================================
Inner Loop
         inprev->data = 22, incur->data = 32, innext->data = 53, intail->data = 47, cur->data = 47, prev->data = 53, next->data = 29
========= Dump List 0x1661090 ===========
         0  19  22  32  53  47  29  116  4  6
===================================
Inner Loop
         inprev->data = 32, incur->data = 53, innext->data = 47, intail->data = 47, cur->data = 47, prev->data = 53, next->data = 29
========= Dump List 0x1661090 ===========
         0  19  22  32  53  47  29  116  4  6
===================================
Before insert mid nodes
         inprev->data = 32, incur->data = 53, innext->data = 47, intail->data = 47, cur->data = 47, prev->data = 53, next->data = 29
========= Dump List 0x1661090 ===========
         0  19  22  32  53  47  29  116  4  6
===================================
After insert mid nodes
========= Dump List 0x1661090 ===========
         0  19  22  32  47  53  29  116  4  6
===================================
无序区
         prev->data = 53, cur->data = 29, next->data = 116, head->data = 0
========= Dump List 0x1661090 ===========
         0  19  22  32  47  53  29  116  4  6
===================================
inprev->data = 0, incur->data = 19, innext->data = 22, intail->data = 29, cur->data = 29
Inner Loop
         inprev->data = 0, incur->data = 19, innext->data = 22, intail->data = 29, cur->data = 29, prev->data = 53, next->data = 116
========= Dump List 0x1661090 ===========
         0  19  22  32  47  53  29  116  4  6
===================================
Inner Loop
         inprev->data = 19, incur->data = 22, innext->data = 32, intail->data = 29, cur->data = 29, prev->data = 53, next->data = 116
========= Dump List 0x1661090 ===========
         0  19  22  32  47  53  29  116  4  6
===================================
Inner Loop
         inprev->data = 22, incur->data = 32, innext->data = 47, intail->data = 29, cur->data = 29, prev->data = 53, next->data = 116
========= Dump List 0x1661090 ===========
         0  19  22  32  47  53  29  116  4  6
===================================
Before insert mid nodes
         inprev->data = 22, incur->data = 32, innext->data = 47, intail->data = 29, cur->data = 29, prev->data = 53, next->data = 116
========= Dump List 0x1661090 ===========
         0  19  22  32  47  53  29  116  4  6
===================================
After insert mid nodes
========= Dump List 0x1661090 ===========
         0  19  22  29  32  47  53  116  4  6
===================================
无序区
         prev->data = 53, cur->data = 116, next->data = 4, head->data = 0
========= Dump List 0x1661090 ===========
         0  19  22  29  32  47  53  116  4  6
===================================
inprev->data = 0, incur->data = 19, innext->data = 22, intail->data = 116, cur->data = 116
Inner Loop
         inprev->data = 0, incur->data = 19, innext->data = 22, intail->data = 116, cur->data = 116, prev->data = 53, next->data = 4
========= Dump List 0x1661090 ===========
         0  19  22  29  32  47  53  116  4  6
===================================
Inner Loop
         inprev->data = 19, incur->data = 22, innext->data = 29, intail->data = 116, cur->data = 116, prev->data = 53, next->data = 4
========= Dump List 0x1661090 ===========
         0  19  22  29  32  47  53  116  4  6
===================================
Inner Loop
         inprev->data = 22, incur->data = 29, innext->data = 32, intail->data = 116, cur->data = 116, prev->data = 53, next->data = 4
========= Dump List 0x1661090 ===========
         0  19  22  29  32  47  53  116  4  6
===================================
Inner Loop
         inprev->data = 29, incur->data = 32, innext->data = 47, intail->data = 116, cur->data = 116, prev->data = 53, next->data = 4
========= Dump List 0x1661090 ===========
         0  19  22  29  32  47  53  116  4  6
===================================
Inner Loop
         inprev->data = 32, incur->data = 47, innext->data = 53, intail->data = 116, cur->data = 116, prev->data = 53, next->data = 4
========= Dump List 0x1661090 ===========
         0  19  22  29  32  47  53  116  4  6
===================================
Inner Loop
         inprev->data = 47, incur->data = 53, innext->data = 116, intail->data = 116, cur->data = 116, prev->data = 53, next->data = 4
========= Dump List 0x1661090 ===========
         0  19  22  29  32  47  53  116  4  6
===================================
无序区
         prev->data = 116, cur->data = 4, next->data = 6, head->data = 0
========= Dump List 0x1661090 ===========
         0  19  22  29  32  47  53  116  4  6
===================================
inprev->data = 0, incur->data = 19, innext->data = 22, intail->data = 4, cur->data = 4
Inner Loop
         inprev->data = 0, incur->data = 19, innext->data = 22, intail->data = 4, cur->data = 4, prev->data = 116, next->data = 6
========= Dump List 0x1661090 ===========
         0  19  22  29  32  47  53  116  4  6
===================================
Before insert mid nodes
         inprev->data = 0, incur->data = 19, innext->data = 22, intail->data = 4, cur->data = 4, prev->data = 116, next->data = 6
========= Dump List 0x1661090 ===========
         0  19  22  29  32  47  53  116  4  6
===================================
After insert mid nodes
========= Dump List 0x1661090 ===========
         0  4  19  22  29  32  47  53  116  6
===================================
========= Dump List 0x1661090 ===========
         0  4  19  22  29  32  47  53  116  6
===================================
inprev->data = 0, incur->data = 4, innext->data = 19, intail->data = 6, cur->data = 6
Inner Loop
========= Dump List 0x1661090 ===========
         0  4  19  22  29  32  47  53  116  6
===================================
Inner Loop
========= Dump List 0x1661090 ===========
         0  4  19  22  29  32  47  53  116  6
===================================
Before insert mid nodes
========= Dump List 0x1661090 ===========
         0  4  19  22  29  32  47  53  116  6
===================================
After insert mid nodes
========= Dump List 0x1661090 ===========
         0  4  6  19  22  29  32  47  53  116
===================================
========= Dump List 0x1661090 ===========
         0  4  6  19  22  29  32  47  53  116
===================================

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值