当前位置: 首页 > news >正文

数据结构-双向链表(核心代码)

#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> typedef int Elemtype; //定义双线链表 typedef struct node { Elemtype data; struct node* next; struct node* prev; }dNode; //初始化双链表 dNode* initlist() { dNode* head = (dNode*)malloc(sizeof(dNode*)); head->data = 0; head->next = NULL; head->prev = NULL; return head; } //头插法-双向链表 int inserthead(dNode* head, Elemtype e) { dNode* P = (dNode*)malloc(sizeof(dNode*)); P->data = e; P->prev = head; P->next = head->next; if (head->next != NULL) { head->next->prev = P; } head->next = P; return 1; } //遍历双向链表 void listdNode(dNode* List) { dNode* P = List->next; while (P != NULL) { printf("%d ", P->data); P = P->next; } printf("\n"); } //尾插法-双向链表 dNode* findtail(dNode* L) { dNode* P = L; while (P->next != NULL) { P = P->next; } return P; } dNode* insertTail(dNode* tail, Elemtype e) { dNode* New = (dNode*)malloc(sizeof(dNode)); New->data = e; New->next = NULL; New->prev = tail; tail->next = New; return New; } //指定位置插入数据 int insertNode(dNode* L, int pos, Elemtype e) { dNode* P = L; int i = 0; while (i < pos - 1) { P = P->next; i++; if (P == NULL) { return 0; } } dNode* Q = (dNode*)malloc(sizeof(dNode)); Q->data = e; Q->prev = P; Q->next = P->next; P->next->prev = Q; P->next = Q; return 1; } //指定位置删除数据 int deleteNode(dNode* L, int pos) { dNode* P = L; int i = 0; while (i < pos - 1) { P = P->next; i++; if (P == NULL) { return 0; } } if (P->next = NULL) { printf("要删除的位置错位\n"); return 0; } dNode* Q = P->next; P->next = Q->next; Q->next->prev = P; free(Q); return 1; } //释放双向链表 dNode* freedNode(dNode* L) { dNode* P = L->next; dNode* Q; while (P != NULL) { Q = P->next; free(P); P = Q; } L->next = NULL; } int main() { dNode* dlist = initlist(); inserthead(dlist, 3); inserthead(dlist, 4); inserthead(dlist, 6); inserthead(dlist, 7); inserthead(dlist, 10); inserthead(dlist, 19); inserthead(dlist, 26); listdNode(dlist); dNode* tail = findtail(dlist); tail = insertTail(tail, 2007); listdNode(dlist); insertNode(dlist, 2, 0); listdNode(dlist); deleteNode(dlist, 2); listdNode(dlist); }#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> typedef int Elemtype; //定义双线链表 typedef struct node { Elemtype data; struct node* next; struct node* prev; }dNode; //初始化双链表 dNode* initlist() { dNode* head = (dNode*)malloc(sizeof(dNode*)); head->data = 0; head->next = NULL; head->prev = NULL; return head; } //头插法-双向链表 int inserthead(dNode* head, Elemtype e) { dNode* P = (dNode*)malloc(sizeof(dNode*)); P->data = e; P->prev = head; P->next = head->next; if (head->next != NULL) { head->next->prev = P; } head->next = P; return 1; } //遍历双向链表 void listdNode(dNode* List) { dNode* P = List->next; while (P != NULL) { printf("%d ", P->data); P = P->next; } printf("\n"); } //尾插法-双向链表 dNode* findtail(dNode* L) { dNode* P = L; while (P->next != NULL) { P = P->next; } return P; } dNode* insertTail(dNode* tail, Elemtype e) { dNode* New = (dNode*)malloc(sizeof(dNode)); New->data = e; New->next = NULL; New->prev = tail; tail->next = New; return New; } //指定位置插入数据 int insertNode(dNode* L, int pos, Elemtype e) { dNode* P = L; int i = 0; while (i < pos - 1) { P = P->next; i++; if (P == NULL) { return 0; } } dNode* Q = (dNode*)malloc(sizeof(dNode)); Q->data = e; Q->prev = P; Q->next = P->next; P->next->prev = Q; P->next = Q; return 1; } //指定位置删除数据 int deleteNode(dNode* L, int pos) { dNode* P = L; int i = 0; while (i < pos - 1) { P = P->next; i++; if (P == NULL) { return 0; } } if (P->next = NULL) { printf("要删除的位置错位\n"); return 0; } dNode* Q = P->next; P->next = Q->next; Q->next->prev = P; free(Q); return 1; } //释放双向链表 dNode* freedNode(dNode* L) { dNode* P = L->next; dNode* Q; while (P != NULL) { Q = P->next; free(P); P = Q; } L->next = NULL; } int main() { dNode* dlist = initlist(); inserthead(dlist, 3); inserthead(dlist, 4); inserthead(dlist, 6); inserthead(dlist, 7); inserthead(dlist, 10); inserthead(dlist, 19); inserthead(dlist, 26); listdNode(dlist); dNode* tail = findtail(dlist); tail = insertTail(tail, 2007); listdNode(dlist); insertNode(dlist, 2, 0); listdNode(dlist); deleteNode(dlist, 2); listdNode(dlist); }
http://www.cnnetsun.cn/news/1300.html

相关文章:

  • PyCharm常用快捷键
  • 玩机搞机基本常识-------全交互式的Android现代反编译器 安卓应用编译工具 强烈推荐
  • 告别API碎片化与高成本 - 用AI Ping打造下一代智能编程工作流
  • 实验记录。
  • SecureCRT SecureFX 9.7 for macOS, Linux, Windows - 跨平台的多协议终端仿真和文件传输
  • Magnet Axiom 9.9 Windows x64 Multilingual - 数字取证与分析
  • JetBrains IDE 2025.3 (macOS, Linux, Windows) - 跨平台开发者工具
  • Nexpose 8.32.0 for Linux Windows - 漏洞扫描
  • Sophos Firewall (SFOS) v22 正式版发布 - 下一代防火墙
  • 【AI专题】一、提示词(prompt)
  • 【AI专题】二、智能体(AI Agent)
  • 计算机毕业设计springboot博主网上招聘系统 基于 SpringBoot 的自媒体人才撮合平台 社交内容创作者在线求职与招聘系统
  • 计算机毕业设计springboot电竞网咖管理系统 基于 SpringBoot 的电竞场馆综合运营平台 融合 SpringBoot 与 Vue 的网咖智慧服务系统
  • 5-36 WPS JS宏综合实例应用-1(多工作表数据合并)
  • 5-37 WPS JS宏综合实例应用-2(一维表变二维表)
  • 5-38 WPS JS宏 综合实例应用-3(按员工职务级别大小排序)
  • 5-39 WPS JS宏 综合实例应用-4(多条件筛选记录并排序)
  • 5-40 WPS JS宏 综合实例应用-5(求字符串中的最大值记录)
  • 【人工智能】【模型】验证大模型效果的终极指南:从人工验证到自动化平台实践
  • TCP通信
  • 三极管应用:直流有刷电机H桥驱动电路——从原理到实践的完整指南
  • 基于NPN三极管的继电器驱动电路:从零开始的嵌入式工程师实战指南
  • 玩转S8050 NPN三极管:从参数选型到实战驱动一篇搞定!
  • 深入浅出玩转S8050:从数据手册到开关/放大电路实战全解析
  • C++数据结构与算法_数据结构与算法概念_定义,递归与迭代比较
  • C++数据结构与算法_数据结构与算法概念_时间复杂度
  • bootstrap前端
  • Python自动化办公全攻略:Excel/Word/PDF/邮件批量处理
  • PyAutoGUI 模拟鼠标键盘:原理解析 + 工程实践案例 + 踩坑指南
  • Python定时任务schedule/APScheduler/Crontab 原理与落地实践