The details of the addition and deletion of nodes in the doubly linked list (what does p->next->prior = s mean, p->next->prior represents the prior pointer of the next node or p itself)

One, write at the beginning

Recently, I was looking at the doubly linked list, and the operation code for adding and deleting nodes in the doubly linked list has troubled me for a long time, so I will write down some of my own insights below, and everyone can tap it .

Second, the relevant code

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct student{
	int data;
	student* prior;
	student* next;
}stu;//如果看不懂上面这段代码的typedef,请参考文章:https://blog.csdn.net/cuizhiyi2008/article/details/102804702 
void output(stu* head,int size){//输出函数 ,按顺序输出链表中各个结点的data 
	cout<<"in output:"<<endl;
	stu *p;
	int count = 1;
	for(p=head; p->next != NULL && count<=20;  p=p->next,count++)
	{
		cout<<"第"<<count<<"个结点,data="<<p->data<<endl;
		//delete p;
	}
	cout<<"第"<<count<<"个结点,data="<<p->data<<endl;
}
void delete_1(stu *head,int position){//删除位于position的结点 
	stu *p;
	int i; 
	for(i=1,p=head;i<position;i++)p=p->next;//让p到达指定位置 

	//从原链表中解绑p所指结点 
	p->prior->next = p->next;
	p->next->prior = p->prior;
	delete p;
	
	output(head,10);//输出操作后的链表 
} 
void insert_1(stu *head,int position,int value){//在第position个结点的前面插入一个结点 
	
	stu *p,*q;//指针p会指向position位置的结点,指针q会指向新结点,到时候新结点的插入就依靠这两个指针 
	int i; 
	for(i=1,p=head;i<position;i++)p=p->next;//让p到达指定位置 

	q = (stu*)malloc(sizeof(stu));
	q->data = value;//给新结点的data赋值 
	//下面这四句代码是插入新结点 
	p->prior->next = q;
	q->prior = p->prior;
	q->next = p;
	q->next->prior = q;
	
	output(head,10);//输出操作后的结果 
	
}
int main(){
	stu *head,*p;//head是链表头指针 
	int i;
	stu text[10];//用结构体数组来描述链表 
	
	p = head = text;
	for(i=0;i<10;i++)//初始化结构体数组 
	{ 
		p->data = i + 10;
		if(i != 0)p->prior = &text[i-1];
		else p->prior = NULL;
		if(i != 9)p->next = &text[i+1];
		else p->next = NULL;
		p = p->next;
		
	}
	cout<<"链表初始内容----------------------------------------------------------------"<<endl; 
	output(head,10);//输出链表中的内容 
	cout<<"第一次插入后----------------------------------------------------------------"<<endl; 
	insert_1(head,5,1111);//在第5个结点位置前面插入一个结点,这个结点的data成员的值为1111 
	cout<<"第二次插入后----------------------------------------------------------------"<<endl; 
	insert_1(head,5,2222);//在前面的基础上,在第5个结点的位置前面插入一个结点,这个结点的data成员的值为2222 
	cout<<"第三次,删除结点后----------------------------------------------------------------"<<endl; 
	delete_1(head,5);
	return 0;
} 

operation result:

 

 Three, related explanations

First, when a new node (set as knot, pointer q points to this node) is inserted before position k in the linked list, assume that pointer p points to the node at position k. Then the content of p->next->prior, the content of p->prior->next, and the content of p, these three are the same, and they are all the addresses of the kth node .

The core code of the insert node function is as follows:

//下面这四句代码是插入新结点 
	p->prior->next = q;
	q->prior = p->prior;
	q->next = p;
	q->next->prior = q;

In the first sentence of code, p->prior->next here represents the next pointer of the k-1th node, not p itself. The two are just the same content, or they point to the same object, so, p ->prior->next->data == p->data. When only p->prior appears, we can understand p->prior as the prior pointer of the node pointed to by p. When p->prior->next appears, we understand p->prior as the node pointed to by p The previous node of the point, that is, the k-1th node, and then p->prior->next is understood as the next pointer of the k-1th node.

Therefore, the function of the first sentence is to assign the address of the new node to the next pointer of the k-1th node.

 When executing the second sentence q->prior = p->prior;, p->prior does not point to the new node, but points to the k-1th node. So the meaning of this code is to assign the address of the k-1th node to the prior pointer of the node pointed to by q.

The third sentence q->next = p; is to let the next pointer of the node pointed to by q point to the kth node (the content of p is the address of the kth node).

The meaning of the fourth sentence q->next->prior = q; is not equivalent to the code q=q;, in fact, here, q->next points to the kth node, so here q-> next can be replaced by p (the content of p is the address of the kth node). But the replaced p->prior does not point to the new node (ie: p->prior cannot be replaced with q), but points to the k-1th node. So this code is to let the prior pointer of the node pointed to by p (kth node) point to q.

After understanding these, then you basically understand the details of adding and deleting doubly linked lists.

Similarly, we can rewrite the core code of the insert function into the following forms:

    q->next = p;
    q->prior = p->prior;
    q->prior->next = q;
    q->next->prior = q;
	q->prior = p->prior;
	p->prior = q;
	q->prior->next = q;
	q->next = p;

Let's see if we can write another one? Practice to deepen your impression and tell everyone in the comment area.

The reference answer is in: 20220729-1 Reference answer_fly_view's blog-CSDN blog

Fourth, write at the end:

If there is any mistake, please correct me, communicate politely, thank you very much

Guess you like

Origin blog.csdn.net/fly_view/article/details/125860496