Merge two circular single chain tables into one circular single chain table

Posted by NDK1971 on Sun, 15 Dec 2019 18:37:27 +0100

Before merger

After merging

Specific ideas:
Define a pointer p to the head node of La to traverse it to the last node (P - > pnext! = LA)
Define a pointer q to the head node of LB to traverse to the last node (P - > pnext! = LB)
LA tail node points to LB head node (the node with the first valid data) (P - > pnext = LB pnext)
Release the head node of lb (free(LB))
At this time, q - > pnext points to the head node that was released before and Q - > pnext points to LA. Then it becomes a cyclic single chain table (Q - > pnext = La - > pnext)

The specific implementation code is as follows

/*
	File name: Example.cpp
	Description: A circular single chain table LA,LB with two leading nodes is written
				Merge two circular single chain tables into one circular single chain table, with the header pointer of LA.
	Author: Yang_Jiang
	Date: 2018 October 13, 2010
	Compiler: Visual Studio 2008
*/

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

typedef struct Student
{
	int id; //Data domain
	struct Student* pNext; //Pointer domain
}NODE,*PNODE;
// NODE is equivalent to struct Student
// PNODE is equivalent to struct Student*


//Function declaration
PNODE create_linlist(int );  //Create linked list
void traverse_linlist(PNODE ); //Traversing linked list
PNODE merge_linlist(PNODE , PNODE);//Merge list

int main()
{
	//Create circular single chain table LA,LB
	PNODE LA = create_linlist(2);
	PNODE LB = create_linlist(2);
	

	//LA after merger
	PNODE P = merge_linlist(LA,LB);
	traverse_linlist(P);
	


	return 0;
}

/*
	Function name: create [linlist();
	Parameter: int len
	Initial condition: no	
	Function: given the length, create a circular single chain table 
	Return value: PNODE 
*/

PNODE create_linlist(int len )
{
	PNODE pHead = (PNODE) malloc(sizeof(NODE) ); //Assign a header node that does not hold valid data
	if( NULL == pHead)
	{
		exit(-1);  //Dynamic memory allocation failed, program terminated!
	}
	else
	{
		PNODE Tail = pHead;
		Tail->pNext = NULL;
		
		//srand((unsigned)time(NULL)); / / the time when the system is called, making the random number assignment different
		for(int i=0; i<len; i++)
		{
			
			PNODE pNew = (PNODE) malloc(sizeof(NODE));
			if( NULL == pNew)
			{	
				exit(-1);
			}
			pNew->id = rand() % 1000; //Random number storage between 0-10000
			
			Tail->pNext = pNew;
			pNew->pNext = NULL;
			Tail = pNew;
		}
		
		Tail->pNext = pHead; //The tail pointer points to the head node, making it a circular single chain table
		
		//Tail->pNext = pHead->pNext;
		//If it's written like this, it points to the head pointer, which is the first valid data
	}

	return pHead;
}

/*
	Function name: traverse? Linlist();
	Parameter: PNODE pS
	Initial condition: the list is not empty
	Function: traverse linked list
	Return value: None 
*/
void traverse_linlist(PNODE pS)
{
	PNODE pre = pS->pNext;
	
	while( pre != NULL)
	{
		printf("%d\t",pre->id);
		pre = pre->pNext;
	}

}


/*
	Function name: merge \ linlist();
	Parameters: pnode La, pnode LB
	Initial condition: LA and LB are cyclic single chain tables
	Function: merge circular single chain table
	Return value: PNODE
*/

PNODE merge_linlist(PNODE LA, PNODE LB )
{
	PNODE p = LA;
	PNODE q = LB;

	while( p->pNext != LA ) //Because it's a circular single chain table. Only when it's not equal to the head node can it represent the last node
	{
		p = p->pNext; //LA traverses to the last node
	}

	while( q->pNext != LB ) //Because it's a circular single chain table. Only when it's not equal to the head node can it represent the last node
	{
		q = q ->pNext; //LB traverses to the last node
	}
	
	p->pNext = LB->pNext;
	free(LB);
	LB = NULL;
	q->pNext = LA->pNext;
	

	return LA;
}





/*
	summary
		Attention should be paid to:
			Tail->pNext = pHead; //The tail pointer points to the head node, making it a circular single chain table
			Tail->pNext = pHead->pNext; //If it's written like this, it points to the head pointer, which is the first valid data
			The condition of judging the last node is that if the linked list points to the head node, it exits the loop. At this time, it must point to the tail node


		This program has been compiled in Visual Studio 2008. The file is a.cpp file. The code is written in a bit nonstandard way, with C language
		The syntax also includes C + + syntax.
*/


Topics: C