当前位置:网站首页>Bidirectional circular linked list (see details)
Bidirectional circular linked list (see details)
2022-04-22 07:36:00 【cxy_ zjt】
Bidirectional circular linked list
preface 
I wrote a blog before, which is a one-way linked list without taking the lead
Now I want to introduce the two-way leading circular linked list , Before the introduction, we know something
- Headless unidirectional acyclic list : Simple structure , Generally, it is not used to store data alone . In fact, it is more as a substructure of other data structures , Such as hash bucket 、 Adjacency table of graph and so on . In addition, this kind of structure appears a lot in the written interview .
- Take the lead in two-way circular list : The most complex structure , It's usually used to store data separately . Linked list data structure used in practice , Are the leading two-way circular list . In addition, although the structure is complex , But after using the code implementation, we will find that the structure will bring many advantages , Instead of
Simple.

It looks something like this , Although the structure is complex , But it's really simple to add, delete, check and change !

Same old thing
This is a List.h Code for Declaration of functions
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode* prev;
struct ListNode* next;
}LTN;
LTN* ListInit();// Initialization of double linked list
LTN* ListBuyNode(LTDataType x);// Create a node
void Listprint(LTN* phead);// Printing of double linked list
void PushBack(LTN* phead, LTDataType x);// Tail insertion
void PuchFront(LTN* phead, LTDataType x);// Head insertion
void ListInsert(LTN* pos, LTDataType x);// stay pos Insert before
void ListErase(LTN* pos);// Delete pos The node of position
void PopFront(LTN* phead);// Head deletion
void PopBack(LTN* phead);// Deletion at the end
LTN* ListFind(LTN* phead, LTDataType x);// lookup x The location of

The first is to write a function that creates a node
This will facilitate the next operation
LTN* ListBuyNode(LTDataType x)// Create a node
{
LTN* newnode = (LTN*)malloc(sizeof(LTN));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
Then is the initialization of the linked list
LTN* ListInit()// Initialization of double linked list
{
LTN* ret = ListBuyNode(0);
ret->next = ret;
ret->prev = ret;
return ret;
}
Printing of linked list
void Listprint(LTN* phead)// Printing of double linked list
{
assert(phead);
LTN* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n\n");
}
Because there is no difficulty , The things to note have been mentioned in the previous sequence table and single chain table
So it's all directly on the code
increase 
Head insertion , Tail insertion , stay pos Insert before
We just need to master the last function —— That is to say pos Just insert this function before , The other two functions call the third function directly
void ListInsert(LTN* pos, LTDataType x)// stay pos Insert before
{
assert(pos);
LTN* ret = ListBuyNode(x);
ret->next = pos;
ret->prev = pos->prev;
pos->prev->next = ret;
pos->prev = ret;
}
void PushBack(LTN* phead, LTDataType x)// Tail insertion
{
assert(phead);
ListInsert(phead, x);
}
void PuchFront(LTN* phead, LTDataType x)// Head insertion
{
assert(phead);
ListInsert(phead->next, x);
}
Delete
Head deletion Deletion at the end Delete pos Location
Empathy Mainly master the third function
void ListErase(LTN* pos)// Delete pos The node of position
{
assert(pos);
LTN* prev = pos->prev;
LTN* next = pos->next;
prev->next = next;
next->prev = prev;
/*pos->next->prev = pos->prev; pos->prev->next = pos->next;*/// Both are OK
free(pos);
pos = NULL;
}

void PopFront(LTN* phead)// Head deletion
{
assert(phead);
ListErase(phead->next);
}
void PopBack(LTN* phead)// Deletion at the end
{
assert(phead);
ListErase(phead->prev);
}
check
The search here is only to search according to the numerical value , It's the same if you look it up by address
LTN* ListFind(LTN* phead, LTDataType x)// lookup x The location of
{
assert(phead);
LTN* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
return cur;
else
cur = cur->next;
}
return NULL;
}
Comrades , The two-way linked list is here

版权声明
本文为[cxy_ zjt]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220612507197.html
边栏推荐
猜你喜欢

2019.1.2 idea usage tutorial

Definition and difference between rewriting and overloading

L1-071 previous life files (20 points) (similar two points)

Minimum circle coverage (basis of computational geometry)

并发编程的艺术(3):深入理解Synchronized的原理

Detailed explanation of linked list

F. Find 3-friendly integers (2021 Niuke summer multi school training camp 1)

A. Alice and Bob (game? Thinking & Violence) (2021 Niuke summer multi school training camp 1)

L2-004 is this a binary search tree? (first order input & judgment search Binary Tree & second order output)

驱动与R3的通信
随机推荐
System log collection series
Yapi的安装与配置(转载)
Codeforces Round #780 (Div. 3)
This关键字详细概述
119 · 编辑距离
FFmpeg命令(七)、 音频与视频合并成视频
Educational Codeforces Round 125 (Rated for Div. 2)
Art de la programmation simultanée (9): utilisation finale et principes
L1-071 前世档案 (20 分) (类似二分)
LeetCode -3 - (字符串相加、最大连续1的个数<ⅠⅢ>、考试的最大困扰度、删除链表的倒数第N个结点)
X64基础(一)
2021 learning plan
B. Ball dropping (simple geometric calculation / similar triangle) (2021 Niuke summer multi school training camp 1)
D. Determine the photo position (simply find the substring) (2021 Niuke summer multi school training camp 1)
E. Figure skiing (string sorting / check-in) (Game 5 of 2021 training League warm-up training competition)
Can the following SQL optimize query performance with index
LeetCode - 5 - (重复的子字符串<kmp>、最长回文子串、转置矩阵、二叉树的(左右)视图)
Codeforces Round #779 (Div. 2)
Kotlin Flow实现线程切换
A. Alice and Bob (博弈?思维&暴力)(2021牛客暑期多校训练营1)