双向链表的增删改查
迪丽瓦拉
2025-05-29 21:29:56
0

1.双向链表的定义

单向链表特点:
  1.我们可以轻松的到达下一个节点, 但是回到前一个节点是很难的.
  2.只能从头遍历到尾或者从尾遍历到头(一般从头到尾)
双向链表特点
  1.每次在插入或删除某个节点时, 需要处理四个节点的引用, 而不是两个. 实现起来要困难一些
  2.相对于单向链表, 必然占用内存空间更大一些.
  3.既可以从头遍历到尾, 又可以从尾遍历到头
双向链表的定义:
  双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。下图为双向链表的结构图。
在这里插入图片描述
  从上中可以看到,双向链表中各节点包含以下 3 部分信息:
  指针域:用于指向当前节点的直接前驱节点;
  数据域:用于存储数据元素。
  指针域:用于指向当前节点的直接后继节点
  在这里插入图片描述
双向循环链表的定义:
  双向链表也可以进行首尾连接,构成双向循环链表,如下图所示
在创建链表时,只需要在最后将收尾相连即可(创建链表代码中已经标出)。其他代码稍加改动即可。
在这里插入图片描述
双链表的节点结构用 C 语言实现为:

/*随机数的范围*/
#define MAX 100
/*节点结构*/
typedef struct Node{struct Node *pre;int data;struct Node *next;
}Node;

2.双向链表的创建

同单链表相比,双链表仅是各节点多了一个用于指向直接前驱的指针域。因此,我们可以在单链表的基础轻松实现对双链表的创建。
  需要注意的是,与单链表不同,双链表创建过程中,每创建一个新节点,都要与其前驱节点建立两次联系,分别是:
  将新节点的 prior 指针指向直接前驱节点;
  将直接前驱节点的 next 指针指向新节点;
  这里给出创建双向链表的 C 语言实现代码:

#define MAX 100
Node *CreatNode(Node *head)
{head=(Node*)malloc(sizeof(Node));//鍒涘缓閾捐〃绗竴涓粨鐐癸紙棣栧厓缁撶偣锛?if(head == NULL){printf("malloc error!\r\n");return NULL;}head->pre=NULL;head->next=NULL;head->data=rand()%MAX;return head;
}
Node* CreatList(Node * head,int length)
{if (length == 1){return( head = CreatNode(head));}else{head = CreatNode(head);Node * list=head;for (int i=1; iNode * body=(Node*)malloc(sizeof(Node));body->pre=NULL;body->next=NULL;body->data=rand()%MAX;/*直接前趋结点的next指针指向新结点*/list->next=body;/*新结点指向直接前趋结点*/body->pre=list;/*把body指针给list返回*/list=list->next;}}/*加上以下两句就是双向循环链表*/// list->next=head;// head->prior=list;return head;
}

3.双向链表的插入

根据数据添加到双向链表中的位置不同,可细分为以下 3 种情况:
1.添加至表头
  将新数据元素添加到表头,只需要将该元素与表头元素建立双层逻辑关系即可。
  换句话说,假设新元素节点为 temp,表头节点为 head,则需要做以下 2 步操作即可:
  temp->next=head; head->prior=temp;
  将 head 移至 temp,重新指向新的表头;
  将新元素 7 添加至双链表的表头,则实现过程如下图所示:
  在这里插入图片描述

2.添加至表的中间位置
  同单链表添加数据类似,双向链表中间位置添加数据需要经过以下 2 个步骤,如下图所示:
  新节点先与其直接后继节点建立双层逻辑关系;
  新节点的直接前驱节点与之建立双层逻辑关系;
在这里插入图片描述

3.添加至表尾
  与添加到表头是一个道理,实现过程如下:
  找到双链表中最后一个节点;
  让新节点与最后一个节点进行双层逻辑关系;
在这里插入图片描述

/*在第add位置的前面插入data节点*/
Node * InsertListHead(Node * head,int add,int data)
{/*新建数据域为data的结点*/Node * temp=(Node*)malloc(sizeof(Node));if(temp== NULL){printf("malloc error!\r\n");return NULL;}    else{temp->data=data;temp->pre=NULL;temp->next=NULL; }/*插入到链表头,要特殊考虑*/if (add==1){temp->next=head;head->pre=temp;head=temp;}else{Node * body=head;/*找到要插入位置的前一个结点*/for (int i=1; ibody=body->next;}/*判断条件为真,说明插入位置为链表尾*/if (body->next==NULL){body->next=temp;temp->pre=body;}else{body->next->pre=temp;temp->next=body->next;body->next=temp;temp->pre=body;}}return head;
}/*在第add位置的后面插入data节点*/
Node * InsertListEnd(Node * head,int add,int data)
{int i = 1;/*新建数据域为data的结点*/Node * temp=(Node*)malloc(sizeof(Node));temp->data=data;temp->pre=NULL;temp->next=NULL;Node * body=head;while ((body->next)&&(ibody=body->next;i++;}/*判断条件为真,说明插入位置为链表尾*/if (body->next==NULL){body->next=temp;temp->pre=body;temp->next=NULL;}else{temp->next=body->pre->next;temp->pre=body->pre;body->next->pre=temp;body->pre->next=temp;}return head;
}

4.双向链表的删除

双链表删除结点时,只需遍历链表找到要删除的结点,然后将该节点从表中摘除即可。
  例如,删除元素 2 的操作过程如图 所示:
在这里插入图片描述

Node * DeleteList(Node * head,int data)
{Node * temp=head;/*遍历链表*/while (temp){/*判断当前结点中数据域和data是否相等,若相等,摘除该结点*/if (temp->data==data) {/*判断是否是头结点*/if(temp->pre == NULL){head=temp->next;temp->next = NULL;free(temp);return head;}/*判断是否是尾节点*/else if(temp->next == NULL){temp->pre->next=NULL;free(temp);return head;}else{temp->pre->next=temp->next;temp->next->pre=temp->pre;free(temp);return head;   }}temp=temp->next;}printf("Can not find %d!\r\n",data);return head;
}

5.双向链表更改节点数据

更改双链表中指定结点数据域的操作是在查找的基础上完成的。实现过程是:通过遍历找到存储有该数据元素的结点,直接更改其数据域即可。

/*更新函数,其中,add 表示更改结点在双链表中的位置,newElem 为新数据的值*/
Node *ModifyList(Node * p,int add,int newElem)
{Node * temp=p;/*遍历到被删除结点*/for (int i=1; itemp=temp->next;}temp->data=newElem;return p;
}

6.双向链表的查找

通常,双向链表同单链表一样,都仅有一个头指针。因此,双链表查找指定元素的实现同单链表类似,都是从表头依次遍历表中元素。

/*head为原双链表,elem表示被查找元素*/
int FindList(Node * head,int elem)
{
/*新建一个指针t,初始化为头指针 head*/Node * temp=head;int i=1;while (temp) {if (temp->data==elem){return i;}i++;temp=temp->next;}/*程序执行至此处,表示查找失败*/return -1;
}

7.双向链表的打印

/*输出链表的功能函数*/
void PrintList(Node * head)
{Node * temp=head;while (temp) {/*如果该节点无后继节点,说明此节点是链表的最后一个节点*/if (temp->next==NULL) {printf("%d\n",temp->data);}else{printf("%d->",temp->data);}temp=temp->next;}
}

测试代码

linkList.h

#include 
//随机数的范围
#define MAX 100//节点结构
typedef struct Node{struct Node *pre;int data;struct Node *next;
}Node;Node* CreatNode(Node *head);//创建一个双向链表的节点
Node* CreatList(Node * head,int length);//创建一串个数为length的双向链表
void PrintList(Node * head);//输出链表的功能函数
Node * InsertListHead(Node * head,int add,int data);//在第add位置的前面插入data节点
Node * InsertListEnd(Node * head,int add,int data);//在第add位置的后面插入data节点
Node * DeleteList(Node * head,int data);//删除数据是data的节点
Node *ModifyList(Node * p,int add,int newElem);//更新函数,其中,add 表示更改结点在双链表中的位置,newElem 为新数据的值
int FindList(Node * head,int elem);//head为原双链表,elem表示被查找元素

linkList.c

#include "linkList.h"
#include 
#include #define MAX 100//创建一个双向链表的节点
Node *CreatNode(Node *head)
{head=(Node*)malloc(sizeof(Node));//申请一个链表节点的空间if(head == NULL){printf("malloc error!\r\n");return NULL;}head->pre=NULL;//指向前一个节点的指针head->next=NULL;//指向后一个节点的指针head->data=rand()%MAX;//随机数 0~100return head;
}//创建一串个数为length的双向链表
Node* CreatList(Node * head,int length)
{if (length == 1){return( head = CreatNode(head));}else{head = CreatNode(head);//先创建一个链表节点作为链表头Node * list=head;//定义一个链表指针指向该链表头for (int i=1; iNode * body = NULL;body = CreatNode(body);//再初始化一个链表节点/*直接前趋结点的next指针指向新结点*/list->next=body;//链表指针的前一个(指针)与新节点连/*新结点指向直接前趋结点*/body->pre=list;//新节点的后一个(指针)与上一个节点相连/*把body指针给list返回*/list=list->next;}}/*加上以下两句就是双向循环链表*/// list->next=head;// head->prior=list;return head;//返回该链表的链表头
}//输出链表的功能函数
void PrintList(Node * head)
{Node * temp=head;while (temp) {//如果该节点无后继节点,说明此节点是链表的最后一个节点if (temp->next==NULL) {printf("%d\n",temp->data);}else{printf("%d->",temp->data);}temp=temp->next;}
}//在第add位置的前面插入data节点
Node * InsertListHead(Node * head,int add,int data)
{//新建数据域为data的结点Node * temp=(Node*)malloc(sizeof(Node));if(temp== NULL){printf("malloc error!\r\n");return NULL;}    else{temp->data=data;temp->pre=NULL;temp->next=NULL; }//插入到链表头,要特殊考虑if (add==1){temp->next=head;head->pre=temp;head=temp;}else{Node * body=head;//找到要插入位置的前一个结点for (int i=1; ibody=body->next;}//判断条件为真,说明插入位置为链表尾if (body->next==NULL){body->next=temp;temp->pre=body;}else{body->next->pre=temp;temp->next=body->next;body->next=temp;temp->pre=body;}}return head;
}//在第add位置的后面插入data节点
Node * InsertListEnd(Node * head,int add,int data)
{int i = 1;//新建数据域为data的结点Node * temp=(Node*)malloc(sizeof(Node));temp->data=data;temp->pre=NULL;temp->next=NULL;Node * body=head;while ((body->next)&&(ibody=body->next;i++;}//判断条件为真,说明插入位置为链表尾if (body->next==NULL){body->next=temp;temp->pre=body;temp->next=NULL;}else{temp->next=body->pre->next;temp->pre=body->pre;body->next->pre=temp;body->pre->next=temp;}return head;
}//删除数据是data的节点
Node * DeleteList(Node * head,int data)
{Node * temp=head;//遍历链表while (temp){//判断当前结点中数据域和data是否相等,若相等,摘除该结点if (temp->data==data) {//判断是否是头结点if(temp->pre == NULL){head=temp->next;temp->next = NULL;free(temp);return head;}//判断是否是尾节点else if(temp->next == NULL){temp->pre->next=NULL;free(temp);return head;}else{temp->pre->next=temp->next;temp->next->pre=temp->pre;free(temp);return head;   }}temp=temp->next;}printf("Can not find %d!\r\n",data);return head;
}//更新函数,其中,add 表示更改结点在双链表中的位置,newElem 为新数据的值
Node *ModifyList(Node * p,int add,int newElem)
{Node * temp=p;//遍历到被删除结点for (int i=1; itemp=temp->next;}temp->data=newElem;return p;
}//head为原双链表,elem表示被查找元素
int FindList(Node * head,int elem)
{//新建一个指针t,初始化为头指针 headNode * temp=head;int i=1;while (temp) {if (temp->data==elem){return i;}i++;temp=temp->next;}//程序执行至此处,表示查找失败return -1;
}

main.c

#include "linkList.h"int main() 
{Node * head=NULL;//创建双链表head=CreatList(head,5);printf("新创建双链表为\t");PrintList(head);//在表中第 5 的位置插入元素 1head=InsertListHead(head, 5,1);printf("在表中第 5 的位置前面插入元素 1\t");PrintList(head);//在表中第 3 的位置插入元素 7head=InsertListEnd(head, 3, 7);printf("在表中第 3 的位置后面插入元素 7\t");PrintList(head);// //表中删除元素 7head=DeleteList(head, 7);printf("表中删除元素 7\t\t\t");PrintList(head);printf("元素 1 的位置是\t:%d\n",FindList(head,1));//表中第 3 个节点中的数据改为存储 6head = ModifyList(head,3,6);printf("表中第 3 个节点中的数据改为存储6\t");PrintList(head);return 0;
}

测试结果

在这里插入图片描述

相关内容

热门资讯

linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
C++ 机房预约系统(六):学... 8、 学生模块 8.1 学生子菜单、登录和注销 实现步骤: 在Student.cpp的...
A.机器学习入门算法(三):基... 机器学习算法(三):K近邻(k-nearest neigh...
数字温湿度传感器DHT11模块... 模块实例https://blog.csdn.net/qq_38393591/article/deta...
有限元三角形单元的等效节点力 文章目录前言一、重新复习一下有限元三角形单元的理论1、三角形单元的形函数(Nÿ...
Redis 所有支持的数据结构... Redis 是一种开源的基于键值对存储的 NoSQL 数据库,支持多种数据结构。以下是...
win下pytorch安装—c... 安装目录一、cuda安装1.1、cuda版本选择1.2、下载安装二、cudnn安装三、pytorch...
MySQL基础-多表查询 文章目录MySQL基础-多表查询一、案例及引入1、基础概念2、笛卡尔积的理解二、多表查询的分类1、等...
keil调试专题篇 调试的前提是需要连接调试器比如STLINK。 然后点击菜单或者快捷图标均可进入调试模式。 如果前面...
MATLAB | 全网最详细网... 一篇超超超长,超超超全面网络图绘制教程,本篇基本能讲清楚所有绘制要点&#...
IHome主页 - 让你的浏览... 随着互联网的发展,人们越来越离不开浏览器了。每天上班、学习、娱乐,浏览器...
TCP 协议 一、TCP 协议概念 TCP即传输控制协议(Transmission Control ...
营业执照的经营范围有哪些 营业执照的经营范围有哪些 经营范围是指企业可以从事的生产经营与服务项目,是进行公司注册...
C++ 可变体(variant... 一、可变体(variant) 基础用法 Union的问题: 无法知道当前使用的类型是什...
血压计语音芯片,电子医疗设备声... 语音电子血压计是带有语音提示功能的电子血压计,测量前至测量结果全程语音播报࿰...
MySQL OCP888题解0... 文章目录1、原题1.1、英文原题1.2、答案2、题目解析2.1、题干解析2.2、选项解析3、知识点3...
【2023-Pytorch-检... (肆十二想说的一些话)Yolo这个系列我们已经更新了大概一年的时间,现在基本的流程也走走通了,包含数...
实战项目:保险行业用户分类 这里写目录标题1、项目介绍1.1 行业背景1.2 数据介绍2、代码实现导入数据探索数据处理列标签名异...
记录--我在前端干工地(thr... 这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前段时间接触了Th...
43 openEuler搭建A... 文章目录43 openEuler搭建Apache服务器-配置文件说明和管理模块43.1 配置文件说明...