目录
我们观察双向链表的结构可以发现一个节点不仅可以指向后一个节点,同时也可以指向前一个节点。
这里的head跟在之前的单链表中我们说的“头节点”是两个概念,实际在之前的在单链表阶段称呼不严谨,但是为了让我们更好的理解就直接称为单链表的头节点。
带头链表⾥的头节点,实际为“哨兵位”,哨兵位节点不存储任何有效元素,只是站在这里放哨的。
“哨兵位”存在的意义: 遍历循环链表避免死循环。
双向链表的结构体:
- typedef int LTDataType;
- typedef struct ListNode
- {
- struct ListNode* next; //指针保存下⼀个节点的地址
- struct ListNode* prev; //指针保存前⼀个节点的地址
- LTDataType data;
- }LTNode;
结构体函数的实现:
初始化的过程中需要我们创建一个哨兵位,这就又需要我们申请一个节点,由于以后的函数也要用到申请节点的代码所以我们将申请节点独立封装成一个函数:
- LT* LTBuyNode(LTDataType x)
- {
- LT* node = (LT*)malloc(sizeof(LT));
- if (node == NULL)
- {
- perror("malloc");
- exit(1);
- }
- node->data = x;
- node->next = node->prev = node;
- }
然后直接使用这个函数完成链表的初始化就可以了。
- void LTInit(LT** pphead)
- {
- *pphead = LTBuyNode(-1);
- }
尾部插入首先需要我们申请一个新节点,再将其插入到双向链表的尾部,这里为了不让插入后影响head节点和d3节点的指向,所以先我们就先将newnode节点连接d3节点和head节点上面。
- void LTPushBack(LT* phead, LTDataType x)
- {
- assert(phead);
- LT* newnode = LTBuyNode(x);
- newnode->next = phead;
- newnode->prev = phead->prev;
- phead->prev->next = newnode;
- phead->prev = newnode;
- }
注意:这段代码最后两行代码不可以交换位置,不然会造成phead->prev的数据提前丢失的问题。
为了证明我们写的代码的正确性,我们将其打印出来。
- void LTPrint(LT* phead)
- {
- LT* ptail = phead->next;
- while (ptail != phead)
- {
- printf("%d->", ptail->data);
- ptail = ptail->next;
- }
- printf("\n");
- }
- void LTPushFront(LT* phead, LTDataType x)
- {
- assert(phead);
- LT* newnode = LTBuyNode(x);
- newnode->next = phead->next;
- newnode->prev = phead;
- phead->next->prev = newnode;
- phead->next = newnode;
- }
尾删中的断言需要排除链表必须有效和链表必须不为空的情况(只有一个哨兵位)。
- void LTPopBack(LT* phead)
- {
- assert(phead&&phead!=phead->next);
- LT* del = phead->prev;
- del->prev->next = phead;
- phead->prev = del->prev;
- free(del);
- del = NULL;
- }
- void LTPopFront(LT* phead)
- {
- assert(phead && phead != phead->next);
- //链表必须有效和链表必须不为空的情况(只有一个哨兵位)
- LT* del = phead->next;
- del->next->prev = phead;
- phead->next = del->next;
- free(del);
- del = NULL;
- }
- LT* LTFind(LT* phead, LTDataType x)
- {
- assert(phead && phead != phead->next);
- LT* ptail = phead->next;
- while (ptail != phead)
- {
- if (ptail->data == x)
- {
- return ptail;
- }
- ptail = ptail->next;
- }
- return NULL;
- }
- void LTInsert(LT* pos, LTDataType x)
- {
- assert(pos);
- LT* del = pos;
- LT* newnode = LTBuyNode(x);
- newnode->next = del->next;
- newnode->prev = del;
- del->next->prev = newnode;
- del->next = newnode;
- }
- void LTErase(LT* pos)
- {
- assert(pos);
- LT* del = pos;
- del->prev->next = del->next;
- del->next->prev = del->prev;
- free(del);
- del = NULL;
- }
在销毁链表的时候我们可以直接调用头删的函数
- void LTDesTroy(LT* phead)
- {
- assert(phead);
- LT* ptail = phead->next;
- while (ptail != phead)
- {
- LTPopFront(phead);
- ptail = phead->next;
- }
- free(ptail);
- ptail = NULL;
- }
整体展示:
test.c
- #define _CRT_SECURE_NO_WARNINGS 1
- #include"List.h"
-
- void test01()
- {
- LT* plist = NULL;
- LTInit(plist);
- //输入操作的指令
- LTDesTroy(plist);
- }
- int main()
- {
- test01();
- return 0;
- }
List.c
- #define _CRT_SECURE_NO_WARNINGS 1
- #include"List.h"
- //申请节点
- LT* LTBuyNode(LTDataType x)
- {
- LT* node = (LT*)malloc(sizeof(LT));
- if (node == NULL)
- {
- perror("malloc");
- exit(1);
- }
- node->data = x;
- node->next = node->prev = node;
- }
- void LTInit(LT** pphead)
- {
- *pphead = LTBuyNode(-1);
- }
- void LTPushBack(LT* phead, LTDataType x)
- {
- assert(phead);
- LT* newnode = LTBuyNode(x);
- newnode->next = phead;
- newnode->prev = phead->prev;
- phead->prev->next = newnode;
- phead->prev = newnode;
- }
- void LTPrint(LT* phead)
- {
- LT* ptail = phead->next;
- while (ptail != phead)
- {
- printf("%d->", ptail->data);
- ptail = ptail->next;
- }
- printf("\n");
- }
- void LTPushFront(LT* phead, LTDataType x)
- {
- assert(phead);
- LT* newnode = LTBuyNode(x);
- newnode->next = phead->next;
- newnode->prev = phead;
- phead->next->prev = newnode;
- phead->next = newnode;
- }
- void LTPopBack(LT* phead)
- {
- assert(phead&&phead!=phead->next);
- LT* del = phead->prev;
- del->prev->next = phead;
- phead->prev = del->prev;
- free(del);
- del = NULL;
- }
- void LTPopFront(LT* phead)
- {
- assert(phead && phead != phead->next);
- //链表必须有效和链表必须不为空的情况(只有一个哨兵位)
- LT* del = phead->next;
- del->next->prev = phead;
- phead->next = del->next;
- free(del);
- del = NULL;
- }
- LT* LTFind(LT* phead, LTDataType x)
- {
- assert(phead && phead != phead->next);
- LT* ptail = phead->next;
- while (ptail != phead)
- {
- if (ptail->data == x)
- {
- return ptail;
- }
- ptail = ptail->next;
- }
- return NULL;
- }
-
- void LTInsert(LT* pos, LTDataType x)
- {
- assert(pos);
- LT* del = pos;
- LT* newnode = LTBuyNode(x);
- newnode->next = del->next;
- newnode->prev = del;
- del->next->prev = newnode;
- del->next = newnode;
- }
- void LTErase(LT* pos)
- {
- assert(pos);
- LT* del = pos;
- del->prev->next = del->next;
- del->next->prev = del->prev;
- free(del);
- del = NULL;
- }
- void LTDesTroy(LT* phead)
- {
- assert(phead);
- LT* ptail = phead->next;
- while (ptail != phead)
- {
- LTPopFront(phead);
- ptail = phead->next;
- }
- free(ptail);
- ptail = NULL;
- }
List.h
- #pragma once
- #include
- #include
- #include
- typedef int LTDataType;
- typedef struct ListNode
- {
- struct ListNode* next; //指针保存下⼀个节点的地址
- struct ListNode* prev; //指针保存前⼀个节点的地址
- LTDataType data;
- }LT;
-
- //初始化
- void LTInit(LT** pphead);
- //尾插
- void LTPushBack(LT* phead, LTDataType x);
- //打印链表
- void LTPrint(LT* phead);
- //头插
- void LTPushFront(LT* phead, LTDataType x);
- //尾删
- void LTPopBack(LT* phead);
- //头删
- void LTPopFront(LT* phead);
- //查找
- LT* LTFind(LT* phead, LTDataType x);
- //在指定位置之后插入节点
- void LTInsert(LT* pos, LTDataType x);
- //删除指定位置的节点
- void LTErase(LT* pos);
- //销毁链表
- void LTDesTroy(LT* phead);