当前位置:网站首页>Double linked list of linear list
Double linked list of linear list
2022-07-03 11:20:00 【jiuqi_ Jiu Qi】
Definition of double linked list
typedef struct DNode {
int data;
struct DNode *prior, *next;
} DNode, *DLinkList;
Initialization of double linked list
bool InitDLinkList(DLinkList &L) {
L = (DNode *) malloc(sizeof(DNode));
if (L == NULL) return false;
L->prior = NULL;
L->next = NULL;
return true;
}
Double linked list empty judgment operation
bool Empty(DLinkList L) {
return L->next == NULL;
}
Establishment of double linked list
The first interpolation
DLinkList HeadInsert(DLinkList &L) {
InitDLinkList(L);
int x;
scanf("%d", &x);
while (x != 9999) {
DNode *s = (DNode *) malloc(sizeof(DNode));
s->data = x;
if (L->next == NULL) {
// Determine whether there are other nodes
s->next = NULL;
L->next = s;
s->prior = L;
} else {
s->next = L->next;
L->next->prior = s;
L->next = s;
s->prior = L;
}
scanf("%d", &x);
}
return L;
}
The tail interpolation
DLinkList TailInsert(DLinkList &L) {
InitDLinkList(L);
DNode *r = L;
int x;
scanf("%d", &x);
while (x != 9999) {
DNode *s = (DNode *) malloc(sizeof(DNode));
s->data = x;
s->next = NULL;
s->prior = r;
r->next = s;
r = s;
scanf("%d", &x);
}
return L;
}
Insertion of double linked list
The tail interpolation
// To insert by tail insertion
void TailInsertList(DLinkList &L, int i, int e) {
DNode *p = GetElem(L, i);
//printf(" The first %d Value of bit order = %d\n", i, p->data);
DNode *s = (DNode *) malloc(sizeof(DNode));
s->data = e; // Assign to new s node
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
p->next = s;
s->prior = p;
}
Double linked list search
Search by bit
DNode *GetElem(DLinkList L, int i) {
int j = 1;
DNode *p = L->next;
if (i == 0)return L;
if (i < 1)return NULL;
while (p && j < i) {
p = p->next;
j++;
}
return p;
}
According to the value lookup
DNode *LocateElem(DLinkList L, int x) {
DNode *p = L->next;
while (p && p->data != x) {
p = p->next;
}
return p;
}
Deletion of double linked list
// Delete p The successor node of the node
bool DeleteNextDNode(DLinkList &L, int i) {
DNode *p = GetElem(L, i-1);
DNode *q = p->next;
printf("p=%d\n", q->data);
if (q == NULL) return false;
p->next = q->next;
if (p->next != NULL)
q->next->prior = p;
free(q);
return true;
}
Traversal of double linked list
void PrintList(DLinkList L) {
DNode *p = L->next;
while (p) {
printf("%d\t", p->data);
p = p->next;
}
printf("\n");
}
Be careful
Among the values searched in bit order , i Is the order
Insert ( Tail insertion ) In operation , Insert bit order to i Behind , GetElem(L, i);
Delete ( Deletion at the end ) In operation , Delete the bit order as i Value , That is to find the bit order i-1 The node of ,GetElem(L, i-1);
Special attention should be paid to the critical value ! ! !
Complete code
/** * @Author JiaHaoHao * @Date 2022/07/01 17:18 * @ClassName: test12 * @Description: Double linked list ( Leading node ) */
#include "stdio.h"
#include "stdlib.h"
typedef struct DNode {
int data;
struct DNode *prior, *next;
} DNode, *DLinkList;
// initialization
bool InitDLinkList(DLinkList &L) {
L = (DNode *) malloc(sizeof(DNode));
if (L == NULL) return false;
L->prior = NULL;
L->next = NULL;
return true;
}
// Determine whether it is null
bool Empty(DLinkList L) {
return L->next == NULL;
}
// stay p Insert after the node s node
bool InsertNextDNode(DNode *p) {
DNode *s = (DNode *) malloc(sizeof(DNode));
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
s->prior = p;
p->next = s;
}
// Head insertion method to establish double linked list
DLinkList HeadInsert(DLinkList &L) {
InitDLinkList(L);
int x;
scanf("%d", &x);
while (x != 9999) {
DNode *s = (DNode *) malloc(sizeof(DNode));
s->data = x;
if (L->next == NULL) {
// Determine whether there are other nodes
s->next = NULL;
L->next = s;
s->prior = L;
} else {
s->next = L->next;
L->next->prior = s;
L->next = s;
s->prior = L;
}
scanf("%d", &x);
}
return L;
}
// Tail insertion method to create double linked list
DLinkList TailInsert(DLinkList &L) {
InitDLinkList(L);
DNode *r = L;
int x;
scanf("%d", &x);
while (x != 9999) {
DNode *s = (DNode *) malloc(sizeof(DNode));
s->data = x;
s->next = NULL;
s->prior = r;
r->next = s;
r = s;
scanf("%d", &x);
}
return L;
}
// Traversal operation
void PrintList(DLinkList L) {
DNode *p = L->next;
while (p) {
printf("%d\t", p->data);
p = p->next;
}
printf("\n");
}
// According to the value lookup
DNode *LocateElem(DLinkList L, int x) {
DNode *p = L->next;
while (p && p->data != x) {
p = p->next;
}
return p;
}
// Search by bit
DNode *GetElem(DLinkList L, int i) {
int j = 1;
DNode *p = L->next;
if (i == 0)return L;
if (i < 1)return NULL;
while (p && j < i) {
p = p->next;
j++;
}
return p;
}
// To insert by tail insertion
void TailInsertList(DLinkList &L, int i, int e) {
DNode *p = GetElem(L, i);
printf(" The first %d Value of bit order = %d\n", i, p->data);
DNode *s = (DNode *) malloc(sizeof(DNode));
s->data = e; // Assign to new s node
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
p->next = s;
s->prior = p;
}
// Delete p The successor node of the node
bool DeleteNextDNode(DLinkList &L, int i) {
DNode *p = GetElem(L, i - 1);
DNode *q = p->next;
printf("p=%d\n", q->data);
if (q == NULL) return false;
p->next = q->next;
if (p->next != NULL)
q->next->prior = p;
free(q);
return true;
}
int main() {
DLinkList L;
// HeadInsert(L); // Head insertion
TailInsert(L); // Tail insertion
PrintList(L);
//printf(" Bit order search : %d\n", GetElem(L, 1));
//(" value finding : %d\n", LocateElem(L, 1));
//DeleteNextDNode(L, 4); // Delete operation
TailInsertList(L, 3, 8);
PrintList(L);
return 0;
}
边栏推荐
- 在腾讯云容器服务Node上执行 kubectl
- How did I grow up in the past eight years as a test engineer of meituan? I hope technicians can gain something after reading it
- 线性表顺序表综合应用题P18
- How to become a senior digital IC Design Engineer (1-4) Verilog coding syntax: expression
- 解决undefined reference to `__aeabi_uidivmod‘和undefined reference to `__aeabi_uidiv‘错误
- C语言 AES加解密
- Driver development based on I2C protocol
- Word line and bit line
- 程序员的创业陷阱:接私活
- Reading notes: heart like Bodhi, Cao Dewang
猜你喜欢
用了这么久线程池,你真的知道如何合理配置线程数吗?
php服务器 与redis交互大量CLOSE_WAIT分析
Stack, monotone stack, queue, monotone queue
行业唯一!法大大电子合同上榜36氪硬核科技企业
面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩
Solution: jupyter notebook does not pop up the default browser
MATLAB提取不規則txt文件中的數值數據(簡單且實用)
MATLAB提取不规则txt文件中的数值数据(简单且实用)
"Core values of testing" and "super complete learning guide for 0 basic software testing" summarized by test engineers for 8 years
00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
随机推荐
多维度监控:智能监控的数据基础
Application of high-precision indoor positioning technology in safety management of smart factory
栈,单调栈,队列,单调队列
asyncio 警告 DeprecationWarning: There is no current event loop
Driver development based on I2C protocol
Error installing the specified version of pilot
(2) Base
The five-year itch of software testing engineers tells the experience of breaking through bottlenecks for two years
程序员的创业陷阱:接私活
Illustrated network: what is virtual router redundancy protocol VRRP?
线性表顺序表综合应用题P18
项目管理精华读书笔记(六)
C language log base zlog basic use
Intel 13th generation core flagship exposure, single core 5.5ghz
I, a tester from a large factory, went to a state-owned enterprise with a 50% pay cut. I regret it
AMS series - application startup process
用了这么久线程池,你真的知道如何合理配置线程数吗?
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
1. Hal driven development
Matlab memory variable management command