当前位置:网站首页>线性表的双链表
线性表的双链表
2022-07-03 09:45:00 【jiuqi_玖柒】
双链表的定义
typedef struct DNode {
int data;
struct DNode *prior, *next;
} DNode, *DLinkList;
双链表的初始化
bool InitDLinkList(DLinkList &L) {
L = (DNode *) malloc(sizeof(DNode));
if (L == NULL) return false;
L->prior = NULL;
L->next = NULL;
return true;
}
双链表的判空操作
bool Empty(DLinkList L) {
return L->next == NULL;
}
双链表的建立
头插法
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) {
//判断是否还有其他结点
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;
}
尾插法
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;
}
双链表的插入
尾插法
//尾插法插入
void TailInsertList(DLinkList &L, int i, int e) {
DNode *p = GetElem(L, i);
//printf("第%d位序的值 = %d\n", i, p->data);
DNode *s = (DNode *) malloc(sizeof(DNode));
s->data = e; //赋值给新的s结点
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
p->next = s;
s->prior = p;
}
双链表的查找
按位查找
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;
}
按值查找
DNode *LocateElem(DLinkList L, int x) {
DNode *p = L->next;
while (p && p->data != x) {
p = p->next;
}
return p;
}
双链表的删除
//删除p结点的后继节点
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;
}
双链表的遍历
void PrintList(DLinkList L) {
DNode *p = L->next;
while (p) {
printf("%d\t", p->data);
p = p->next;
}
printf("\n");
}
注意
在按位序查找的值中, i 为第几个位序
插入(尾插)操作中, 插入在位序给 i 的后面, GetElem(L, i);
删除(尾删)操作中, 删除位序为 i 的值, 即要找到位序 i-1 的结点,GetElem(L, i-1);
需特别注意临界值的情况 ! ! !
完整代码
/** * @Author JiaHaoHao * @Date 2022/07/01 17:18 * @ClassName: test12 * @Description: 双链表(带头结点) */
#include "stdio.h"
#include "stdlib.h"
typedef struct DNode {
int data;
struct DNode *prior, *next;
} DNode, *DLinkList;
//初始化
bool InitDLinkList(DLinkList &L) {
L = (DNode *) malloc(sizeof(DNode));
if (L == NULL) return false;
L->prior = NULL;
L->next = NULL;
return true;
}
//判断是否为空
bool Empty(DLinkList L) {
return L->next == NULL;
}
//在p结点之后插入s结点
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;
}
//头插法建立双链表
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) {
//判断是否还有其他结点
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;
}
//尾插法建立双链表
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;
}
//遍历操作
void PrintList(DLinkList L) {
DNode *p = L->next;
while (p) {
printf("%d\t", p->data);
p = p->next;
}
printf("\n");
}
//按值查找
DNode *LocateElem(DLinkList L, int x) {
DNode *p = L->next;
while (p && p->data != x) {
p = p->next;
}
return p;
}
//按位查找
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;
}
//尾插法插入
void TailInsertList(DLinkList &L, int i, int e) {
DNode *p = GetElem(L, i);
printf("第%d位序的值 = %d\n", i, p->data);
DNode *s = (DNode *) malloc(sizeof(DNode));
s->data = e; //赋值给新的s结点
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
p->next = s;
s->prior = p;
}
//删除p结点的后继节点
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); //头插
TailInsert(L); //尾插
PrintList(L);
//printf("位序查找: %d\n", GetElem(L, 1));
//("数值查找: %d\n", LocateElem(L, 1));
//DeleteNextDNode(L, 4); //删除操作
TailInsertList(L, 3, 8);
PrintList(L);
return 0;
}
边栏推荐
- . Net core - a queuing system for wechat official account
- 12. Nacos server service registration of source code analysis of Nacos service registration
- ConstraintLayout跟RelativeLayout嵌套出现的莫名奇妙的问题
- QT:QSS自定义QGroupBox实例
- Latest sales volume of pinduoduo
- Flink chain conditional source code analysis
- Qt:qss custom QSlider instance
- 10. Nacos source code construction
- Internet Socket (非)阻塞write/read n个字节
- 字节跳动大裁员,测试工程师差点遭团灭:大厂招人背后的套路,有多可怕?
猜你喜欢
软件测试必学基本理论知识——APP测试
What are the strengths of "testers"?
QT: QSS custom qtabwidget and qtabbar instances
QT: QSS custom qtableview instance
I have been doing software testing for three years, and my salary is less than 20K. Today, I put forward my resignation
Probability theory: application of convolution in calculating moving average
Qt:qss custom qpprogressbar instance
Game test related tests a hero's skills (spring moves are asked more questions)
Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?
QT: QSS custom qtreeview instance
随机推荐
QT: QSS custom qtableview instance
C language project: student achievement system
测试Leader应该做哪些事
Game test related tests a hero's skills (spring moves are asked more questions)
Logstash backup tracks the data records reported
Multiple IO transfer - preamble
12. Nacos server service registration of source code analysis of Nacos service registration
值得关注的15种软件测试趋势
QT:QSS自定义QGroupBox实例
如何让让别人畏惧你
软件测试工程师的5年之痒,讲述两年突破瓶颈经验
年中了,准备了少量的自动化面试题,欢迎来自测
Error installing the specified version of pilot
How does MySQL find the latest data row that meets the conditions?
我,大厂测试员,降薪50%去国企,后悔了...
Flink chain conditional source code analysis
. Net core - a queuing system for wechat official account
QT: QSS custom qtreeview instance
How to realize automatic testing in embedded software testing?
ExecutorException: Statement returned more than one row, where no more than one was expected.