news 2026/5/1 7:31:32

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

作者头像

张小明

前端开发工程师

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进行投诉反馈,一经查实,立即删除!