Introduction to data structure - detailed explanation of linked list_Doubly linked list

foreword

Introduction to data structure - detailed explanation of two-way linked list
Blog homepage link: https://blog.csdn.net/m0_74014525
Follow the blogger, and the series of articles will be continuously updated in the later period.
There is source code at the end of the article
*****感谢观看,希望对你有所帮助*****


series of articles

Part 1: Introduction to Data Structure — Detailed Explanation of Linked List_Single Linked List
Part 2: Introduction to Data Structure — Detailed Explanation of Linked List_Doubly Linked List
Part 3: Introduction to Data Structure — Detailed Explanation of Linked List_Circular Linked List



What is a doubly linked list

Doubly Linked List (Doubly Linked List) is a linked list data structure, each of its nodes contains two pointers, one pointing to the previous node, one pointing to the next node. Compared with the singly linked list, the doubly linked list can be traversed in both directions, that is, the linked list can be traversed from the beginning to the end or from the end to the beginning. In a doubly linked list, each node contains two pointer fields and one data field. Among them, one pointer points to the predecessor node, and the other pointer points to the successor node. These two pointers make the operations of inserting and deleting the doubly linked list unnecessary to traverse the entire linked list to find the precursor nodes like the one-way linked list, which improves the operation efficiency of the linked list.

Concept and structure (graphic)

insert image description here
Leading two-way circular linked list: the most complex structure, generally used to store data separately. The linked list data structure used in practice is a two-way circular linked list with the lead. In addition, although the structure is complex, after using the code to implement it, you will find that the structure will bring many advantages, and the implementation is simple. We will know when we implement the code later.

The difference between doubly linked list and singly linked list

Doubly linked list and singly linked list are two different linked list structures.

A singly linked list is a linked list that contains in each node a pointer to the next node. This means that in a singly linked list, nodes can only be traversed from the beginning to the end. In a singly linked list, each node only stores a pointer to the next node, not a pointer to the previous node.

A doubly linked list is a linked list that contains in each node pointers to the next node and the previous node. This means that in a doubly linked list, nodes can be traversed either from head to tail or from tail to head. In a doubly linked list, each node stores pointers to the previous node and the next node.

Therefore, a doubly linked list can be more convenient for two-way traversal, but requires more memory space to store two pointers for each node. In contrast, in a singly linked list, only one pointer is needed to point to the next node, so the memory footprint is smaller.

Take the lead in the implementation of the two-way circular linked list interface (code demonstration)

带头+双向+循环链表增删查改实现

1. Dynamic storage structure

typedef int STDataType;
typedef struct ListNode
{
    
    
	struct ListNode* prev;
	struct ListNode* next;
	STDataType data;
}LTNode;

Doubly linked list printing

void LTPrint(LTNode* phead)
{
    
    
	assert(phead);
	printf("phead<->");
	//跳过哨兵位
	LTNode* cur = phead->next;
	while (cur != phead)
	{
    
    
		printf("%d<->", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

Add, delete, check and modify interface

根据增删查改顺序编排
Doubly linked list header:

//头插
void  LTPushFront(LTNode* phead, STDataType x)
{
    
    
	assert(phead);
	LTNode* newnode = BuyLTNode(x);
	LTNode* first = phead->next;
	newnode->next = first;
	first->prev = newnode;
	phead->next = newnode;
	newnode->prev = phead;

}

Doubly linked list tail insertion:

//尾插
void LTPushBack(LTNode* phead, STDataType x)
{
    
    
	assert(phead);
	
	LTNode* newnode = BuyLTNode(x);
	//找到最后一个
	LTNode* tail = phead->prev;
	
	
	newnode->prev = tail;
	tail->next = newnode;
	

	newnode->next = phead;
	phead->prev = newnode;
}

Doubly linked list header deletion:

//头删
void LTPopFront(LTNode* phead)
{
    
    
	assert(phead);
	assert(phead->next != phead);
	LTNode* first = phead->next;
	LTNode* second = first->next;
	free(first);
	phead->next = second;
	second->prev = phead;

}

Doubly linked list end deletion:

//尾删
void LTPopBack(LTNode* phead)
{
    
    
	assert(phead);
	assert(phead->next != phead);
	LTNode* tail = phead->prev;
	LTNode* tailprev = tail->prev;

	free(tail);
	
	phead->prev = tailprev;
	tailprev->next = phead;

}

Find:

LTNode* LTFind(LTNode* phead, STDataType x)
{
    
    
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead)
	{
    
    
		if (cur->data == x)
		{
    
    
			return cur;
		}
		cur = cur->next;
	}

	return NULL;
}

Insert at the pointed position:

void LTInsert(LTNode* pos, STDataType x)
{
    
    
	LTNode* newnode = BuyLTNode(x);
	LTNode* posprev = pos->prev;
	newnode->next = pos;
	pos->prev = newnode;
	posprev->next = newnode;
	newnode->prev = posprev;

}

Delete at the pointed position:

// 把pos删除
void LTErase(LTNode* pos)
{
    
    
	LTNode* posprev = pos->prev;
	LTNode* posnext = pos->next;
	free(pos);
	posprev->next = posnext;
	posnext->prev = posprev;
}

Doubly linked list destruction

void LTDestory(LTNode* phead)
{
    
    
	LTNode* cur = phead->next;
	while (cur != phead)
	{
    
    
		LTNode* next = cur->next;
		free(cur);
		cur = next;
	}

	free(phead);
	phead = NULL;
}

5. All file codes

1. Gitee link

***查看所有代码***
Click on the blue text on the right DuckBro Gitee code repository


Summarize

Take the lead in the basic concepts and common operations of doubly linked lists. The leading bidirectional circular linked list is a special kind of doubly linked list, which has a head node and a tail node, and the end to end is connected to form a ring.

This enables looping through the linked list. In the leading bidirectional circular linked list, operations such as inserting and deleting nodes have special processing methods. The leading bidirectional circular linked list is more common in practical applications, such as process management in the operating system, playlists in music players, etc.


如这篇博客对大家有帮助的话,希望 三连 支持一下 !!! 如果有错误感谢大佬的斧正 如有 其他见解发到评论区,一起学习 一起进步。

Guess you like

Origin blog.csdn.net/m0_74014525/article/details/132501288