Two-way-->Take the lead-->Circular linked list

Table of contents

1. Overview of two-way headed circular linked list

1. What is a two-way headed circular linked list?

2. Advantages of two-way headed circular linked list

3. Simplified diagram of two-way headed circular linked list

2. Illustration and code implementation of addition, deletion, checking and modification of two-way leading circular linked list

1. The head plug of the two-way headed circular linked list

2. Tail insertion of bidirectional headed circular linked list

3. Head deletion of two-way headed circular linked list

4. Tail deletion of two-way leading circular linked list

5. The two-way headed circular linked list inserts the node before the pos position.

6. Delete the pos position node in the two-way headed circular linked list


1. Overview of two-way headed circular linked list

1. What is a two-way headed circular linked list?

        Bidirectional: Each node has a pointer to the next node (next) and a pointer to the previous node (prev);

        Headed: That is, the linked list has a sentinel head node, which only contains two pointers and does not store valid data;

        Loop: The sentinel bit head node has a next pointer pointing to the first valid data node, and a prev pointer pointing to the previous node of the sentinel bit node, which is the tail node of the linked list, thus realizing the loop of the linked list;

        Node type of bidirectional headed circular linked list:

typedef int LTDataType;
typedef struct ListNode
{
	LTDataType data;
	struct ListNode* next;
	struct ListNode* prev;
}ListNode;

2. Advantages of two-way headed circular linked list

        The two-way headed circular linked list does not require us to traverse each node to find the tail node, which makes tail insertion of the linked list very simple. Compared with the one-way non-cyclic linked list, the two-way leading circular linked list has an additional pointer prev pointing to the previous node, so it is more complex in structure, but it saves a lot of trouble in practical applications.

3. Simplified diagram of two-way headed circular linked list

 

2. Illustration and code implementation of addition, deletion, checking and modification of two-way leading circular linked list

1. The head plug of the two-way headed circular linked list

Schematic diagram:

Code:

// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* NewNode = Node_New(x);
	ListNode* First = pHead->next;
	NewNode->next = First;
	First->prev = NewNode;
	NewNode->prev = pHead;
	pHead->next = NewNode;
}

2. Tail insertion of bidirectional headed circular linked list

Schematic diagram:

Code:

// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* NewNode = Node_New(x);
	ListNode* Tail = pHead->prev;
	NewNode->prev = Tail;
	Tail->next = NewNode;
	NewNode->next = pHead;
	pHead->prev = NewNode;
}

3. Head deletion of two-way headed circular linked list

Schematic diagram:

Code:

// 双向链表头删
void ListPopFront(ListNode* pHead)
{
	assert(pHead);
	if (pHead->next == pHead)
	{
		return;
	}
	ListNode* First = pHead->next;
	ListNode* Next = First->next;
	pHead->next = Next;
	Next->prev = pHead;
	free(First);
	First = NULL;
}

4. Tail deletion of two-way leading circular linked list

Schematic diagram:

Code:

// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
	assert(pHead);
	if (pHead->next == pHead)
	{
		return;
	}
	ListNode* Tail = pHead->prev;
	ListNode* Prev = Tail->prev;
	Prev->next = pHead;
	pHead->prev = Prev;
	free(Tail);
	Tail = NULL;
}

5. The two-way headed circular linked list inserts the node before the pos position.

Schematic diagram:

Code:

// 双向链表在pos位置的前面插入节点
void ListInsert(ListNode* pos, LTDataType x)
{
	ListNode* NewNode = Node_New(x);
	ListNode* Prev = pos->prev;
	Prev->next = NewNode;
	NewNode->prev = Prev;
	NewNode->next = pos;
	pos->prev = NewNode;
}

6. Delete the pos position node in the two-way headed circular linked list

Schematic diagram:

Code:

// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{
	ListNode* Prev = pos->prev;
	ListNode* Hind = pos->next;
	Prev->next = Hind;
	Hind->prev = Prev;
	free(pos);
	pos = NULL;
}

Guess you like

Origin blog.csdn.net/libj2023/article/details/132338989