合并两个循环链表

#include"circularlist.h"
int main(void)
{
     

	int i = 0;

	CircularNode* p = NULL;

	CircularNode* q = NULL;

	//创建clist1循环链表

	circularlinklist* clist1 = (circularlinklist*)malloc(sizeof(circularlinklist));//头指针指向头节点

	CircularNode* k = NULL;

	clist1->length = 0;//用于记录链表长度

	clist1->next = NULL;

	for (i = 1; i < 10; i++)
	{
     
		insertcircularlinklist(clist1, i, i);//插入节点
	}
	
	printcircularlinklist(clist1);//打印节点

	printf("\n");


	//创建clist2循环链表
	circularlinklist* clist2 = (circularlinklist*)malloc(sizeof(circularlinklist));//头指针指向头节点

	clist2->length = 0;//用于记录链表长度

	clist2->next = NULL;

	for (i = 1; i < 10; i++)
	{
     
		insertcircularlinklist(clist2, i, i+9);//插入节点
	}

	printcircularlinklist(clist2);//打印节点

	printf("\n");


	p = getlastnode(clist1);//用来保存clist1链表的尾节点地址

	q = getlastnode(clist2);//用来保存clist2链表的尾节点地址

	printf("\n");

	mergeTwoLists(clist1, clist2, p, q);//合并两个链表


	printcircularlinklist1(clist1,clist2);//打印合并后的循环链表

    printf("\n");


	system("pause");

	return 0;
}
void insertcircularlinklist(circularlinklist* clist, int pos, int value)
{
     
	int i = 0;
	//创建一个空节点

	CircularNode *node = (CircularNode*)malloc(sizeof(CircularNode));

	node->data = value;

	node->next = NULL;

	if (pos == 1)//插入的位置为一
	{
     
		//printf("%d\n", clist->next);

		//node->next = clist->next;

		if (!node->next)//插入的链表为0
		{
     

			node->next = node;//单个节点指向自己

		}

		else
		{
     

			CircularNode *lastnode = clist->next;

			for (i = 1; i < clist->length; i++)
			{
     


				lastnode=lastnode->next;

			}

			lastnode->next = node;

		}

		clist->next = node;

		clist->length++;

		return;

	}

	CircularNode *currnode = clist->next;//从第一个结点开始

	for (i = 1; currnode&&i < pos - 1; i++)//开始遍历节点,找到当前节点
	{
     

		currnode = currnode->next;

	}

	if (currnode)//当前节点非空
	{
     

		node->next = currnode->next;//改变指向

		currnode->next = node;

		clist->length++;//链表长度加一

		if (pos == clist->length)
		{
     
			node->next = clist->next;//最后一个节点指向第一个节点

		}
	}
}

void printcircularlinklist(circularlinklist* clist)
{
     
	int i = 0;

	if (clist->length == 0 || !clist->next)
	{
     

		printf("链表的长度为空,无法打印!\n");

		clist->length = 0;

		return;

	}

	CircularNode* node = clist->next;//头指针指向头节点

	for (i = 0; i < clist->length; i++)//循环打印节点数据
	{
     
		printf("%d\t", node->data);

		node = node->next;     
	}

	printf("\n");

}

void  mergeTwoLists(circularlinklist* clist1, circularlinklist* clist2, CircularNode* rearA, CircularNode* rearB)
{
     
	CircularNode* p = NULL;

	circularlinklist* q = NULL;

	p = clist1->next;

	rearA->next = rearB->next;

	q = clist2;

	rearB->next = p;

	//free(clist2);

}

void printcircularlinklist1(circularlinklist* clist1, circularlinklist* clist2)
{
     
	int i = 0;

	CircularNode* node = clist1->next;//头指针指向头节点

	for (i = 0; i < clist1->length+ clist2->length; i++)//循环打印节点数据
	{
     
		printf("%d\t", node->data);

		node = node->next;
	}

	printf("\n");

}

typedef int datatype;

//循环链表的结点 
typedef struct circularNode
{
      
	datatype data;//数据域

	struct circularNode *next;//指针域

} CircularNode;



//循环链表的结构
typedef struct circularlinklist  //头节点
{
     

	CircularNode* next;  //指向头节点的指针(头指针)

	int length;

}circularlinklist;

//注:这里链表的尾节点指针域指向链表的第一个节点(不是头节点)
合并两个链表的关键是利用好两个链表的尾指针,通过改变它们的指向即可进行链表的合并。
合并两个循环链表_第1张图片

你可能感兴趣的:(数据结构与算法)