Chapter 3: 4. Circular Linked List and Doubly Linked List

This article refers to "Dahua Data Structure", thanks to the author Mr. Cheng Jie.

One: Circular linked list:
Change the pointer end of the terminal node in the singly linked list from a null pointer to point to the head node, which makes the entire singly linked list form a ring.
This kind of singly linked list is called a single circular linked list, abbreviated as a single linked list. Circular linked list (circular linked list).

Illustration:
Insert picture description here
Two: Combine the following two single circular linked lists:

Insert picture description here
The process is as follows:

p = rearA->next;					// 保存A表的头结点,即① 
rearA->next = rearB->next->next;	//将本是指向B表的第一个节点(不是头结点),赋值给 rearA->next,即② 
rearB->next = p;					//将原A表的头结点赋值给 rearB->next,即③ 

free(p);							//释放p

Three: double linked list:
In each node of the singly linked list, set a pointer to its predecessor node. Therefore, the nodes in the doubly linked list have two pointer fields,
one points to the immediate successor, and one points to the immediate predecessor.

/*线性表的双向链表存储结构*/
typedef struct DulNode
{
    
    
		ElemType data;
		struct DuLNode *prior;    	/*直接前驱指针*/
		struct DuLNode *next;		/*直接后继指针*/
} DulNode, *DuLinkList;

Four: The doubly linked list of non-empty loop leading nodes is as follows:
Insert picture description here
Just like life, you have to work hard if you want to have fun, and you have to pay a price if you want to gain.
Since the doubly linked list has more data structures such as reverse traversal lookup than the singly linked list, you have to pay some price.
When inserting and deleting, you need to change two pointer variables.

Five: Insertion of double-linked list: The
insertion operation is not complicated, but the order is very important, and it must not be wrong.

Assuming that the node of the storage element e is s, the following steps are required to insert the node s between the nodes p and p->next:
Insert picture description here

There are four steps in total:

s->prior 		= p;		//把p赋值给s的前驱,如图中1
s->next 		= p->next;	//把p->next赋值给s的后继,如图中2
p->next->prior 	= s;		//把s赋值给p->next的前驱,如图中3
p-next 			= s;		//把s赋值给p的后继,如图中4

Technique 1: The order of assignment (double-arm telescopic method)
1. Expand the left arm;
2. Expand the right arm;
3. Retract the right arm;
4. Retract the left arm; it
just corresponds to the 4 arrows in the figure above.

Tip 2: Assignment of each step (see arrow pointing)
A ------------------------------------- ----------------------------> B
Predecessor/successor of A (depending on where the arrow starts from A) = B (direct Write the identification of the node);

Six: Deletion of double-linked list:
To delete the node p, only the following two steps are required: There are
Insert picture description here
three steps in total:

p->prior->next = p->next;	//把p->next赋值给p->prior的后继,如图1
p->next->prior = p->prior;	//把p->prior赋值给p->next的前驱,如图2

free(p);					//释放节点p

Note:
1. The order of deletion is up and down (arrow);
2. Technique 2 is also applicable to deletion;

Guess you like

Origin blog.csdn.net/yanghangwww/article/details/110305261