1.双向链表的结构
注意:这⾥的“带头”跟前⾯我们说的“头节点”是两个概念,实际前⾯的在单链表阶段称呼不严 谨,但是为了同学们更好的理解就直接称为单链表的头节点。
带头链表⾥的头节点,实际为“哨兵位”,哨兵位节点不存储任何有效元素,只是站在这⾥“放哨的”
“哨兵位”存在的意义: 遍历循环链表避免死循环。
2.双向链表的实现
代码地址
LIst.h
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode* prev;
struct ListNode* next;
};
//初始化
struct ListNode* LTInit();
//销毁
void LTDesTroy(struct ListNode* phead);
void LTPrint(struct ListNode* phead);
//尾插
//不改变哨兵位的地址,因此传一级即可
void LTPushBack(struct ListNode* phead, LTDataType x);
//头插
void LTPushFront(struct ListNode* phead, LTDataType x);
//尾删
void LTPopBack(struct ListNode* phead);
//头删
void LTPopFront(struct ListNode* phead);
//在pos位置之后插入数据
void LTInsert(struct ListNode* pos, LTDataType x);
//删除pos节点
void LTErase(struct ListNode* pos);
struct ListNode* LTFind(struct ListNode* phead, LTDataType x);
List.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"List.h"
void LTPrint(struct ListNode* phead)
{
struct ListNode* pcur = phead->next;
while (pcur != phead)
{
printf("%d->", pcur->data);
pcur = pcur->next;
}
printf("\n");
}
//申请节点
struct ListNode* LTBuyNode(LTDataType x)
{
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
if (node == NULL)
{
perror("malloc fail!");
exit(1);
}
node->data = x;
node->next = node->prev = node;
return node;
}
//初始化
//void LTInit(LTNode** pphead)
//{
// //给双向链表创建一个哨兵位
// *pphead = LTBuyNode(-1);
//}
struct ListNode* LTInit()
{
struct ListNode* phead = LTBuyNode(-1);
return phead;
}
//尾插
void LTPushBack(struct ListNode* phead, LTDataType x)
{
assert(phead);
struct ListNode* newnode = LTBuyNode(x);
//phead phead->prev newnode
newnode->prev = phead->prev;
newnode->next = phead;
phead->prev->next = newnode;
phead->prev = newnode;
}
//头插
void LTPushFront(struct ListNode* phead, LTDataType x)
{
assert(phead);
struct ListNode* newnode = LTBuyNode(x);
//phead newnode phead->next
newnode->next = phead->next;
newnode->prev = phead;
phead->next->prev = newnode;
phead->next = newnode;
}
//尾删
void LTPopBack(struct ListNode* phead)
{
//链表必须有效且链表不能为空(只有一个哨兵位)
assert(phead && phead->next != phead);
struct ListNode* del = phead->prev;
//phead del->prev del
del->prev->next = phead;
phead->prev = del->prev;
//删除del节点
free(del);
del = NULL;
}
//头删
void LTPopFront(struct ListNode* phead)
{
assert(phead && phead->next != phead);
struct ListNode* del = phead->next;
//phead del del->next
phead->next = del->next;
del->next->prev = phead;
//删除del节点
free(del);
del = NULL;
}
struct ListNode* LTFind(struct ListNode* phead, LTDataType x)
{
struct ListNode* pcur = phead->next;
while (pcur != phead)
{
if (pcur->data == x)
{
return pcur;
}
pcur = pcur->next;
}
//没有找到
return NULL;
}
//在pos位置之后插入数据
void LTInsert(struct ListNode* pos, LTDataType x)
{
assert(pos);
struct ListNode* newnode = LTBuyNode(x);
//pos newnode pos->next
newnode->next = pos->next;
newnode->prev = pos;
pos->next->prev = newnode;
pos->next = newnode;
}
//删除pos节点
void LTErase(struct ListNode* pos)
{
//pos理论上来说不能为phead,但是没有参数phead,无法增加校验
assert(pos);
//pos->prev pos pos->next
pos->next->prev = pos->prev;
pos->prev->next = pos->next;
free(pos);
pos = NULL;
}
void LTDesTroy(struct ListNode* phead)
{
assert(phead);
struct ListNode* pcur = phead->next;
while (pcur != phead)
{
struct ListNode* next = pcur->next;
free(pcur);
pcur = next;
}
//此时pcur指向phead,而phead还没有被销毁
free(phead);
phead = NULL;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"List.h"
void ListTest01()
{
//LTNode* plist = NULL;
//LTInit(&plist);
struct ListNode* plist = LTInit();
LTPushBack(plist, 1);
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPrint(plist);
struct ListNode* find = LTFind(plist, 3);
//LTInsert(find, 66);
LTErase(find);
find = NULL;
LTPrint(plist);
LTDesTroy(plist);
//plist = NULL;
//if (find == NULL)
//{
// printf("找不到!\n");
//}
//else {
// printf("找到了!\n");
//}
//LTPushFront(plist, 1);
//LTPrint(plist);
//LTPushFront(plist, 2);
//LTPrint(plist);
//LTPushFront(plist, 3);
//
//
测试尾删
//LTPopBack(plist);
//LTPrint(plist);
//LTPopBack(plist);
//LTPrint(plist);
//LTPopBack(plist);
//LTPrint(plist);
测试头删
//LTPopFront(plist);
//LTPrint(plist);
//LTPopFront(plist);
//LTPrint(plist);
//LTPopFront(plist);
//LTPrint(plist);
//LTPopBack(plist);
//LTPrint(plist);
}
int main()
{
ListTest01();
return 0;
}