news 2026/6/23 20:30:49

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
数据结构-双向链表(核心代码)
#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); }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/22 20:29:47

PyCharm常用快捷键

1、按住CtrlQ或者CtrlShiftI快捷键可以查看函数定义 2、ShiftF6更改文件名 3、CtrlShiftF10运行程序

作者头像 李华
网站建设 2026/6/22 23:26:35

玩机搞机基本常识-------全交互式的Android现代反编译器 安卓应用编译工具 强烈推荐

在反编译一些安卓应用过程中。我们大都使用一些常用的反编译工具。例如手机端的mt管理器与电脑端各种成品类工具。但目前发现的这款反编译利器必须强烈推荐给大家使用。它不只是一款反编译器&#xff0c;同时也是一款轻便且功能强大的综合性逆向分析利器&#xff0c;其不依赖ja…

作者头像 李华
网站建设 2026/6/23 13:44:34

告别API碎片化与高成本 - 用AI Ping打造下一代智能编程工作流

AI Ping官方链接&#xff1a;https://aiping.cn/#?channel_partner_codeGQCOZLGJ 点击上述链接注册&#xff0c;将获得30元算力金&#xff0c; 小伙伴们快速体验AI Ping强大的功能吧~ 前言 在人工智能技术飞速发展的今天&#xff0c;AI编程助手已成为开发者提升效率的利器。…

作者头像 李华
网站建设 2026/6/23 16:17:41

实验记录。

Fi配了大半天&#xff0c;结果半天用木…。抓了一个后抓其他的可能导致网连不上&#xff0c;不知道是配置的问题&#xff0c;还是软件就是如此&#xff0c;运行过程中不能开其他的软件吗&#xff1f;以后试试吧。你开启Proxifier后其他软件出现异常&#xff0c;核心原因是Proxi…

作者头像 李华
网站建设 2026/6/23 1:43:20

Magnet Axiom 9.9 Windows x64 Multilingual - 数字取证与分析

Magnet Axiom 9.9 Windows x64 Multilingual - 数字取证与分析 Digital Forensic Software 请访问原文链接&#xff1a;https://sysin.org/blog/magnet-axiom/ 查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.org Magnet Axiom 在一个案件中恢…

作者头像 李华