欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

双向循环链表的插入 -- C语言

程序员文章站 2024-03-22 11:46:40
...

代码实现

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

	st_doubNode * head = NULL;
	st_doubNode * iter = NULL;
	st_doubNode * p = NULL;
	
	head = *phead;
	if(NULL == head){ /*新加入的节点*/
		p = (st_doubNode * ) malloc (sizeof(st_doubNode));
		if(NULL == p){
			printf("%s: alloc error\n",__func__);
			return ALLOC_ERR;
		}
		p->data = data;
		p->next = p;
		p->prev = p;	
		head = p;
	}

	iter = head;
	while(iter->next != head){
		if(data < iter->data){
			/*分配节点*/
			p = (st_doubNode * ) malloc (sizeof(st_doubNode));
			if(NULL == p){
				printf("%s: alloc error\n",__func__);
				return ALLOC_ERR;
			}		
			p->data = data;
			p->next = NULL;
			p->prev = NULL;

			/*插入*/
			iter->prev->next = p;
			p->prev = iter->prev;
			iter->prev = p;
			p->next = iter;			
			
			if(head == iter){ /*头之前插入,更新头*/
				head = p;
			}
			
			goto out;
		}
		iter = iter->next;
	}

	/*尾巴处理, 此时iter指向尾巴*/
	if(NULL == p){ 
		/*分配节点*/
		p = (st_doubNode * ) malloc (sizeof(st_doubNode));
		if(NULL == p){
			printf("%s: alloc error\n",__func__);
			return ALLOC_ERR;
		}
		p->data = data;
		p->next = NULL;
		p->prev = NULL;		

		/*插在倒数第二个节点*/
		if(data < iter->data){
			iter->prev->next = p;
			p->prev = iter->prev;
			p->next = iter;
			iter->prev = p;
		} else { /*插在最后一个节点*/
			iter->next = p;
			p->prev = iter;
			p->next = head;			
		}		
	}

out:
	*phead = head;
	return SUCCESS;
}


st_doubNode * getDouLoopListTail(st_doubNode* head){
	if(NULL == head ){
		printf("%s: param error\n",__func__);
		return NULL;
	}
	
	st_doubNode * tail = NULL;
	tail = head;
	while(head != tail->next){
		tail = tail->next;
	}

	return tail;
}


void testinsertDoubLoopSortedListNode(void){

	st_doubNode * tail = NULL;

	/*构建双向循环链表*/
	tail = getDouListTail(gDoubHead);
	tail->next = gDoubHead;
	gDoubHead->prev = tail;

	insertDoubLoopSortedListNode(&gDoubHead, 22);
	insertDoubLoopSortedListNode(&gDoubHead, 21);
	insertDoubLoopSortedListNode(&gDoubHead, 0);
	insertDoubLoopSortedListNode(&gDoubHead, 63);
	insertDoubLoopSortedListNode(&gDoubHead, 119);

	/*打开换,因为tail可能会变化,所以要重新取*/
	tail = getDouLoopListTail(gDoubHead);
	tail->next = NULL;
	gDoubHead->prev = NULL;

	dumpDoubList(gDoubHead);
	dumpDoubListReverse(gDoubHead);

	return;
}

代码编译

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

调试输出

========= Dump Double List 0x149a090 ===========
         1  4  6  19  22  29  32  47  53  116
===================================
========= dump DoubList Reverse 0x149a0f0 ===========
         116  53  47  32  29  22  19  6  4  1
================================================
========= Dump Double List 0x149a1f0 ===========
         0  1  4  6  19  21  22  22  29  32  47  53  63  116  119
===================================
========= dump DoubList Reverse 0x149a230 ===========
         119  116  63  53  47  32  29  22  22  21  19  6  4  1  0