当前位置:网站首页>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;
}
边栏推荐
- 今晚要修稿子準備發佈。但是,仍卡在這裡,也許你需要的是一個段子。
- Software testing redis database
- BI技巧丨权限轴
- ConstraintLayout跟RelativeLayout嵌套出现的莫名奇妙的问题
- Processes and threads
- The role and necessity of implementing serializable interface
- 10. Nacos source code construction
- Commonly used discrete random distribution
- One hot code
- The testing department of the company came to the king of the Post-00 roll, and the veteran exclaimed that it was really dry, but
猜你喜欢
基于I2C协议的驱动开发
10. Nacos source code construction
I, a tester from a large factory, went to a state-owned enterprise with a 50% pay cut. I regret it
I have been doing software testing for three years, and my salary is less than 20K. Today, I put forward my resignation
在职美团测试工程师的这八年,我是如何成长的,愿技术人看完都有收获
面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩
做软件测试三年,薪资不到20K,今天,我提出了辞职…
Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3
EPS电动转向系统分析
Intel 13th generation core flagship exposure, single core 5.5ghz
随机推荐
Word line and bit line
Function details of CorelDRAW graphics suite 2022
Internet Socket (非)阻塞write/read n个字节
The element form shows the relationship between elementary transformation and elementary matrix
Stack, monotone stack, queue, monotone queue
栈,单调栈,队列,单调队列
Software testing e-commerce projects that can be written into your resume, don't you come in and get it?
How to become a senior digital IC Design Engineer (1-2) Verilog coding syntax: Verilog 1995, 2001, 2005 standards
如何成为一名高级数字 IC 设计工程师(1-5)Verilog 编码语法篇:操作数
浅析-JMM内存模型
php服务器 与redis交互大量CLOSE_WAIT分析
在职美团测试工程师的这八年,我是如何成长的,愿技术人看完都有收获
00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
ExecutorException: Statement returned more than one row, where no more than one was expected.
2021 postgraduate entrance examination mathematics 2 linear algebra
如何成为一名高级数字 IC 设计工程师(1-2)Verilog 编码语法篇:Verilog 1995、2001、2005 标准
ORACLE 11G 单机冷备数据库
Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
搭建ADG后,实例2无法启动 ORA-29760: instance_number parameter not specified
软件测试工程师的5年之痒,讲述两年突破瓶颈经验