Data structure and algorithm - creation and addition of short linked list

Posted by fxb9500 on Mon, 27 Dec 2021 01:42:39 +0100

introduce

characteristic

1) The first node does not contain data as the chain header (always unchanged), and there is data from the second node
2) Do not use tail pointer, involving tail, circular search; The purpose is different from what I wrote before. It's more interesting!
3) The operation principle and operation logic are basically the same as those of the empty linked list. With more empty heads, the code will be slightly different
4) The difference between the short list and the non short list: some operations with short lists are simpler than those without short lists

Creation of linked list

Creation of short

	//Create a short linked list node separately
	struct Chain Head = {0,NULL};
	//perhaps
	struct Chain *pHead = (struct Chain*)malloc(sizeof(struct Chain));
	pHead->iData = 0;
	pHead->pNext = NULL;
	free(pHead);

The above two methods of creating short positions can be the same. The difference is release: the first release starts from the second node and the second release starts from scratch.
Difference from struct Chain *Head: * Head is a pointer and Head is a space

Establishment of node counter

	struct Chain Head = {0,NULL};
	int iCount = 0; //count

Addition of linked list

Tail addition

1) Transmission parameter
Add and modify the data of the header itself, so it is necessary to transfer the address of the variable header; Add a node counter and modify it, so you need to pass its address; There is also data to add

void AddTail(struct Chain *Head,int *iCount,int iData){}

2) Parameter validity detection
The head must exist! NULL means that it does not exist; The counter cannot be less than 0 or absent

//Parameter validity detection
	if (NULL == Head || NULL == iCount || *iCount < 0)
		return;

3) Create node

struct Chain *pTemp = (struct Chain*)malloc(sizeof(struct Chain));

4) Node assignment

		pTemp->iData = iData;
		pTemp->pNext = NULL;

5) Link to linked list

		struct Chain*pT = Head;
		while (NULL != pT->pNext )
			pT = pT->pNext;
			//At this point pT points to the tail
		pT->pNext = pTemp;//Connect to the tail

6) Counter self increment

	(*iCount)++;

Total code:

void AddTail(struct Chain *Head, int *iCount, int iData)
{
	if (NULL == Head || NULL == iCount || iCount < 0)
		return;
	struct Chain *pTemp = (struct Chain*)malloc(sizeof(struct Chain));
	if (pTemp != NULL)
	{
		pTemp->iData = iData;
		pTemp->pNext = NULL;
		struct Chain*pT = Head;
		while (NULL != pT->pNext )
			pT = pT->pNext;
		pT->pNext = pTemp;
		(*iCount)++;
	}
}

Release linked list

thinking

To change the linked list and number, you need to transfer the chain header and number variables. Similar to the non empty linked list, you need to record the current node, move the node down and release the node. Finally, don't forget to empty it

void FreeChain(struct Chain *Head, int *iCount){
	//Legitimacy detection
	if (NULL == Head || 0 == *iCount )
		return;
	//Do not release the header node
	struct Chain *pTemp = Head->pNext ;
	if (pTemp == NULL)
		return;
	//Cyclic release
	while (pTemp != NULL){
		//Record current node
		struct Chain *pT = pTemp;
		//Node down
		pTemp = pTemp->pNext;
		//Release node
		free(pT );
	}
	Head->pNext  = NULL;
	*iCount = 0;
}

Traversal linked list

Just be careful not to pass the head

//You can also pass values here
void print(struct Chain *Head,int *iCount){
	if (NULL == Head)
		return;
	//Define traversal variables
	struct Chain *pTemp = Head;
	//Loop traversal
	while (pTemp->pNext  != NULL){
	//Skip over node
		printf("%d ",pTemp->pNext ->iData );
		pTemp = pTemp->pNext;
	}
	//Number of output records
	printf("\n share%d Nodes\n",*iCount);
}

Header addition

Idea: add directly at the next position of the head; The new node is connected to the next position of the head, and a new node is added after the head. In front of the last added node: the new node is connected to the head, the head points to the original node, and the new node points to the original node.

void AddHead(struct Chain*Head,int iData, int *iCount)
{
	if (NULL == Head || iCount == NULL )
		return;
	//establish
	struct Chain *pTemp = (struct Chain*)malloc(sizeof(struct Chain));
	if (pTemp == NULL)
		return;
	//assignment
	pTemp->pNext = NULL;
	pTemp->iData = iData;
	//connect
	pTemp->pNext = Head->pNext;
	Head->pNext = pTemp;
	//Quantity plus 1
	(*iCount)++;
}

Intermediate addition

Processing ideas and processing logic are basically similar to those of a shortless linked list
Three cases:

Adds a node after the specified data

1)Parameter transfer: modify the linked list, transfer the first level pointer (the head pointer remains unchanged); transfer the count variable, add nodes and record; transfer the linked list data, find the position to be added through the data; and the data to be added.
void AddMid(struct Chain *pHead, int *iCount, int iData,int Data){}

2) Legitimacy detection and looping to find the previous node of linked list data

	if (NULL == pHead || iCount == NULL || *iCount == 0)
		return;
		//Define intermediate variables to traverse the linked list
	struct Chain *pT = pHead->pNext ;
	while (NULL != pT)
	{
		if (pT->iData == Data)//Previous node found
			break;
		pT = pT->pNext;
	}
	//At this time, pT points to the previous node to be found

3) Determine whether to find

//Determine whether to find
	if (NULL == pT)
		printf("not found\n");

4) Then start to apply for nodes, assign nodes, and connect nodes

	//Application node
	struct Chain *pTemp = (struct Chain*)malloc(sizeof(struct Chain));
	if (pTemp == NULL)
		return;
	//Node assignment
	pTemp->iData = iData;
	pTemp->pNext = NULL;
	//connect
	pTemp->pNext = pT->pNext;
	pT->pNext = pTemp;
	(*iCount)++;

5) Because a large linked list can be said to be composed of many small linked lists, every time a node is added in the middle, it can be said to be a header addition.

//Just pass the header. The node is no longer pahead, but the previous node
//This is where the header is added
	AddHead(pT,iCount, iData);

Adds a node at the specified subscript

Similar to the addition of short positions, there is no more description here
(1)

void AddIndex(struct Chain *pHead, int *iCount, int iIndex, int iData)
{
	if (NULL == pHead || NULL == iCount || *iCount < 0)
		return;
	struct Chain *pTemp = (struct Chain *)malloc(sizeof(struct Chain));
	if (pTemp == NULL)
		return;
	pTemp->iData = iData;
	pTemp->pNext = NULL;
	int Sign = 0;
	struct Chain *pT = pHead;
	while (pT != NULL)
	{
		if (Sign == iIndex)
			break;
		pT = pT->pNext;
		Sign++;
	}
	 pTemp->pNext = pT->pNext ;
	 pT->pNext = pTemp;
	 (*iCount)++;
}

(2) The other is based on adding functions to the header
This method has been mentioned when talking about no short linked list

//Function added in header
void AddHead(struct Chain*Head,int iData, int *iCount)
{
	if (NULL == Head || iCount == NULL )
		return;
	//establish
	struct Chain *pTemp = (struct Chain*)malloc(sizeof(struct Chain));
	if (pTemp == NULL)
		return;
	//assignment
	pTemp->pNext = NULL;
	pTemp->iData = iData;
	//connect
	pTemp->pNext = Head->pNext;
	Head->pNext = pTemp;
	(*iCount)++;
}
void AddIndex(struct Chain *pHead, int *iCount, int iIndex, int iData)
{
	if (NULL == pHead || NULL == iCount || *iCount < 0)
		return;
	//Find location
	int Sign = 0;
	struct Chain *pT = pHead;
	while (pT != NULL)
	{
		if (Sign == iIndex)
			break;
		pT = pT->pNext;
		Sign++;
	}
	//Found at this time
	AddHead(pHead,iCount,iData);
}

Add N nodes at the specified subscript position

Above, we already know the method of inserting a node at the specified position. The function of inserting a node N times in a loop is not to insert N nodes.
(1) The most basic method is to directly put the function of adding a node at the specified subscript position in the loop

void MoreiData(struct Chain *pHead, int *iCount, int iIndex,int iData,int Num)
{
	int i = 1;
	while (i <= Num)
	{
		if (NULL == pHead || NULL == iCount || *iCount < 0)
			return;
		struct Chain *pTemp = (struct Chain *)malloc(sizeof(struct Chain));
		if (pTemp == NULL)
			return;
		pTemp->iData = iData;
		pTemp->pNext = NULL;
		int Sign = 0;
		struct Chain *pT = pHead;
		while (pT != NULL)
		{
			if (Sign == iIndex)
				break;
			pT = pT->pNext;
			Sign++;
		}
		pTemp->pNext = pT->pNext;
		pT->pNext = pTemp;
		(*iCount)++;
		i++;
	}
}

(2) Use the function added in the header
The function added in the header is as above

void MoreiData(struct Chain *pHead, int *iCount, int iIndex,int iData,int Num)
{
	int i = 1;
	while (i <= Num)
	{
		if (NULL == pHead || NULL == iCount || *iCount < 0)
			return;
		int Sign = 0;
		struct Chain *pT = pHead;
		while (pT != NULL)
		{
			if (Sign == iIndex)
				break;
			pT = pT->pNext;
			Sign++;
		}
		AddHead(pT, iCount, iData);
		i++;
	}
}

Topics: Algorithm data structure linked list