C语言数据结构-----双向链表增删查改的代码实现

news/2025/2/1 8:42:52/

文章目录

  • 1.初始化双链表
  • 2.创建链表节点
  • 3.打印链表
  • 4.尾插
  • 5.尾删
  • 6.头插
  • 7.头删
  • 8.在pos之前插入
    • 8.1 在pos之前插入(改造头插)
    • 8.2 在pos之前插入(改造尾插)
  • 9.删除pos位置
    • 9.1 删除pos位置(改造尾删)
    • 9.1 删除pos位置(改造头删)
  • 10.查找
  • 11.毁灭


链接: 顺序表(动态顺序表增删查改的代码实现) 链接:
单链表(无头单向不循环)增删查改的代码实现
往期顺序表,单链表的代码实现

完整代码见gitee:
链接: 双向链表增删查改的代码实现

1.初始化双链表

LTNode* LTInit()
{LTNode* phead = CreateLTNode(-1);phead->next = phead;phead->prev = phead;return phead;
}

2.创建链表节点

LTNode* CreateLTNode(LTDataType x)
{LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));if (newnode == NULL){perror("malloc fail");exit(-1);}newnode->val = x;newnode->next = NULL;newnode->prev = NULL;return newnode;
}

3.打印链表

void LTPrint(LTNode* phead)
{assert(phead);printf("哨兵位<=>");LTNode* cur = phead->next;while (cur != phead){printf("%d<=>", cur->val);cur = cur->next;}printf("\n");
}

4.尾插

void LTPushBack(LTNode* phead, LTDataType x)
{
//第一种assert(phead);LTNode* tail = phead->prev;LTNode* newnode = CreateLTNode(x);//phead               tail  newnodetail->next = newnode;newnode->prev = tail;newnode->next = phead;phead->prev = newnode;
//第二种//LTInsert(phead, x);
}

在这里插入图片描述

5.尾删

void LTPopBack(LTNode* phead)
{
//第一种assert(phead);assert(phead->next != phead);LTNode* tail = phead->prev;LTNode* tailPrev = tail->prev;free(tail);tailPrev->next = phead;phead->prev = tailPrev;
//第二种//LTErase(phead->prev);
}

在这里插入图片描述

6.头插

void LTPushFront(LTNode* phead, LTDataType x)
{
//第一种assert(phead);LTNode* newnode = CreateLTNode(x);LTNode* first = phead->next;phead->next = newnode;newnode->prev = phead;newnode->next = first;first->prev = newnode;
//第二种//LTInsert(phead->next, x);
}

在这里插入图片描述

7.头删

void LTPopFront(LTNode* phead)
{
//第一种assert(phead);assert(phead->next != phead);LTNode* first = phead->next;LTNode* second = first->next;phead->next = second;second->prev = phead;free(first);first = NULL;//第二种//LTErase(phead->next);
}

在这里插入图片描述

8.在pos之前插入

void LTInsert(LTNode* pos, LTDataType x)
{assert(pos);LTNode* posPrev = pos->prev;LTNode* newnode = CreateLTNode(x);// posprev newnode posposPrev->next = newnode;newnode->prev = posPrev;newnode->next = pos;pos->prev = newnode;
}

在这里插入图片描述

8.1 在pos之前插入(改造头插)

LTInsert(phead->next, x);

8.2 在pos之前插入(改造尾插)

LTInsert(phead, x);

在这里插入图片描述

9.删除pos位置

void LTErase(LTNode* pos)
{assert(pos);LTNode* posNext = pos->next;LTNode* posPrev = pos->prev;posPrev->next = posNext;posNext->prev = posPrev;free(pos);pos = NULL;
}

在这里插入图片描述

9.1 删除pos位置(改造尾删)

LTErase(phead->prev);

9.1 删除pos位置(改造头删)

LTErase(phead->next);

10.查找

LTNode* LTFind(LTNode* phead, LTDataType x)
{assert(phead);LTNode* cur = phead->next;while (cur != phead){if (cur->val == x){return cur;}cur = cur->next;}return NULL;
}

11.毁灭

void LTDestroy(LTNode* phead)
{assert(phead);LTNode* cur = phead->next;while (cur != phead){LTNode* next = cur->next;free(cur);cur = next;}free(phead);phead = NULL;
}

在这里插入图片描述

完整代码见gitee: 链接:
双向链表增删查改的代码实现


http://www.ppmy.cn/news/1212714.html

相关文章

【Python】一篇带你掌握数据容器之列表

目录 前言&#xff1a; 一、列表 1.列表的定义 2.列表的下标索引 3.列表的常用操作 &#xff08;1&#xff09;index方法&#xff1a;查找某元素的下标 (2)修改特定位置下标的元素 &#xff08;3&#xff09;insert&#xff08;下标&#xff0c;元素&#xff09;方法&a…

C/S架构学习之基于TCP的本地通信(客户机)

基于TCP的本地通信&#xff08;客户机&#xff09;&#xff1a;创建流程&#xff1a;一、创建字节流式套接字&#xff08;socket函数&#xff09;&#xff1a; int sock_fd socket(AF_LOCAL,SOCK_STREAM,0);二、创建客户机和服务器的本地网络信息结构体并填充客户机和服务器本…

RGB颜色空间与BMP格式图片

RGB颜色空间 RGB可以分为两大类&#xff1a;一种是索引形式&#xff0c;一种是像素形式&#xff1a; 索引形式&#xff1a;存储每个像素在调色板中的索引 RGB1&#xff1a;每个像素用1bit表示&#xff0c;调色板中只包含两种颜色&#xff08;黑白&#xff09;RGB4&#xff1a…

Nginx:不同域名访问同一台机器的不同项目

Nginx很简单就可以解决同一台机器同时跑两个或者多个项目&#xff0c;而且都通过域名从80端口走。 以Windows环境下nginx服务为例&#xff0c;配置文件nginx.conf中&#xff0c;http中加上 include /setup/nginx-1.20.1/conf/conf.d/*.conf;删除server部分&#xff0c;完整如…

AI 时代的企业级安全合规策略

目录 漏洞分类管理的流程 安全策略管理 在扫描结果策略中定义细粒度的规则 有效考虑整个组织中的关键漏洞 确保职责分离 尝试组合拳 本文来源&#xff1a;about.gitlab.com 作者&#xff1a;Grant Hickman 在应用程序敏捷研发、敏捷交付的今天&#xff0c;让安全人员跟上…

postman连接数据库

参考&#xff1a;https://blog.csdn.net/qq_45572452/article/details/126620210 1、安装node.js 2、配置环境变量 3、安装xmysql连接数据库cmd窗口输入"npm install -g xmysql"后回车cmd窗口输入"xmysql"后回车,验证xmysql是否安装成功(下图代表安装成功)…

C++(20):new数组时元素个数自动推到

C20在new数组时可以根据初始化列表&#xff0c;自动推到元素个数&#xff1a; #include <iostream> using namespace std;int main() {int *pd new int[]{1,2,3,4};for(auto i 0; i < 4; i){cout<<pd[i]<<endl;}return 0; }运行程序输出&#xff1a; 1…

王道 | 数据结构第一章

目录结构 章节总览 1.0 开篇_数据结构在学什么 1.1_1 数据结构的基本概念 1.1_2 数据结构的三要素 1.2_1 算法的基本概念 1.2_2 算法的时间复杂度 1.2_3 算法的空间复杂度 章节总览 1.0 开篇_数据结构在学什么 1.1_1 数据结构的基本概念 数据&#xff1a; 数据是信息的载…