当前位置:网站首页>线性表的双链表
线性表的双链表
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;
}
边栏推荐
- Software testing redis database
- Qt:qss custom qgroupbox instance
- Error installing the specified version of pilot
- MySQL -- index principle + how to use
- Comment réaliser des tests automatisés pour les tests logiciels embarqués?
- T5 attempt
- Strategic management of project organization
- My understanding of testing (summarized by senior testers)
- 在职美团测试工程师的这八年,我是如何成长的,愿技术人看完都有收获
- logstash备份跟踪上报的数据记录
猜你喜欢

Some abilities can't be learned from work. Look at this article, more than 90% of peers

Probability theory: application of convolution in calculating moving average

软件测试——Redis数据库

Que se passe - t - il ensuite pour ceux qui se sont concentrés sur les tests automatisés?

Activity and fragment lifecycle

12. Nacos server service registration of source code analysis of Nacos service registration

Qt:qss custom qscrollbar instance

Clion debug

8年测试总监的行业思考,看完后测试思维认知更深刻

《通信软件开发与应用》
随机推荐
[true question of the Blue Bridge Cup trials 44] scratch eliminate the skeleton Legion children programming explanation of the true question of the Blue Bridge Cup trials
我对测试工作的一些认识(资深测试人员总结)
What happened to those who focused on automated testing?
独家分析 | 关于简历和面试的真 相
Qt:qss custom QSlider instance
Qt:qss custom qmenu instance
After 8 years of industry thinking, the test director has a deeper understanding of test thinking
Exclusive analysis | truth about resume and interview
有些能力,是工作中学不来的,看看这篇超过90%同行
ExecutorException: Statement returned more than one row, where no more than one was expected.
多路IO转接——前导
软件测试必学基本理论知识——APP测试
Interviewer: what is the internal implementation of the list in redis?
Flink -- built in function (all)
MAUI Developer Day in GCR
Qt:qss custom qradiobutton instance
How to make a blood bar in the game
Qt:qss custom qscrollbar instance
The element form shows the relationship between elementary transformation and elementary matrix
Is pinduogai's sales safe in 2022?