[Data structure] Tail insertion linked list initialization, adding and deleting data at specified positions

1. Use the tail insertion method to initialize the linked list

        One advantage of using the tail insertion method to initialize the linked list is that new data can be accessed from the end of the linked list , so that the linked list is output according to the first-in-first-out principle when traversing the linked list . In this example, we use a structure for data storage:

	typedef struct Node //链表结构体
	{
		int Param;
		struct Node *next;
	}Node;

        Using the tail insertion method, we need to initialize three brothers, namely L, r and p (of course, this name is not necessary, you can choose three at will), the first is the L head node, which does not store data , only as a leading role , as shown in the figure below:

         Among them, the function of the pointer "L" is to permanently point to the head node , so that you can come back if you get lost when traversing, adding and other operations later. At the same time, we also need a pointer "r" to point to the current node :

        How to understand "point to the current node"? For example, when traversing the entire linked list, if the fourth node is traversed, then the pointer "r" will point to the fourth node, which is what it means. Finally, there is the pointer "p", which is only responsible for creating new nodes :

 

        The functions of the three pointers have been introduced, and the next step is the initialization step. The work to be done is to initialize an empty node as the head node, and let r point to the first node:

	Node* L; 
	Node* p; 
	Node* r;

	L = (Node*)malloc(sizeof(Node));
	L->next = NULL;
	if (L == NULL)
		//报错信息
	r = L;

 2. New/tail insertion node

        The first step of adding a node is to generate a node p out of thin air:

        In the second step, insert the parameters you need into the node, and assign the Next pointer to NULL (because there is no next node):

        The third step is to connect the previous node (the empty node pointed to by L in this example) with the newly initialized node:

        Finally, point the pointer "r" to the current node (that is, p):

        In this way, the step of adding a node is completed, and adding a new node in the future is just a matter of repeating the above steps. The code is as follows:

void NodeCreat(int Param)
{
    p = (Node*)malloc(sizeof(Node));
	p->Param = Param;
	p->next = NULL;
	r->next = p; //不可以写成L->next = p,如果不知道为什么,就尝试着插入2个以上的数据看看会发生什么
	r = p;
}

3. Delete the node at the specified position

        There will be many situations in deleting the node at the specified location. We need to take different pre-error measures according to different situations. Let’s first explain the method of deleting the node and initialize a pointer q to find the previous location of the target. Node:

        Subsequent deletion:

         When deleting a node, the following situations will occur:

  • The first node is deleted
  • The deletion is the first node && the first node is just the last node
  • The node in the middle is deleted
  • Delete the middle node && want to delete the last node

        For the above situation, there is the following code:

void DeleteNode(int location)
{
	Node* q = L->next;
	Node* Delq;
	int Tmp_location = 0;

	while (L->next != NULL) //不止有一个节点
	{
		if (Tmp_location == location) //删除第一个节点
		{
			if (L->next->next == NULL) //第一个节点是最后一个节点
			{
				free(L->next);
				L->next = NULL;
				r = L;
				break;
			}

			Delq = q;
			L->next = q->next;
			free(Delq);
			break;
		}
		else if (Tmp_location == location - 1) //删除中间的节点
		{
			if (q->next->next == NULL) //删除最后一个节点
			{
				free(q->next);
				q->next = NULL;
				r = q;
				break;
			}

			Delq = q->next;
			q->next = q->next->next;
			r = q->next;
			free(Delq);
			break;
		}
		else
		{
			q = q->next;
			Tmp_location++;
		}
	}
}

         The above delete node code responds differently to different situations to prevent crashes when deleting nodes, and at the same time keep the pointer r at the current position after deletion to facilitate subsequent operations

Supongo que te gusta

Origin blog.csdn.net/qq_41884002/article/details/129790341
Recomendado
Clasificación