1.双向链表的结构
注:这里的"带头"跟"头节点"是两个概念,单链表节点称呼不严谨。其实,在带头链表中的头节点,实际为"哨兵位",哨兵位节点不存储任何有效元素。"哨兵位"存在的意义是遍历循环链表时避免死循环。
通过名字可以看出,双向链表实际上是带头双向循环链表。相对于之前的单链表来说,带头的意思是存在一个"哨兵位",故双向链表需要初始化,初始化就是创建一个"哨兵位节点",双向的意思表示这个链表既能从前往后也能从后往前,所以在双向链表的结构里面应该也含有一个向前指向的指针prev,为了实现循环的效果,在每次创建新的节点是,该节点的next指针和prev指针都应该指向自己。下列是双向链表节点的结构:
cpp
typedef struct ListNode
{
LTDataType val;
struct ListNode* prev;
struct ListNode* next;
}LTNode;
2.实现双向链表
2.1头文件List.h
cpp
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType val;
struct ListNode* prev;
struct ListNode* next;
}LTNode;
//双向链表为空时,只有一个头节点
void LTInit(LTNode** pphead);
//LTNode* LTInit();
void LTDestroy(LTNode* phead);
void LTPrint(LTNode* phead);
void LTPushBack(LTNode* phead, LTDataType x);
void LTPopBack(LTNode* phead);
void LTPushFront(LTNode* phead, LTDataType x);
void LTPopFront(LTNode* phead);
//在pos位置之后插入数据
void LTInsert(LTNode* pos, LTDataType x);
void LTErase(LTNode* pos);
LTNode* LTFind(LTNode* phead, LTDataType x);
2.2函数实现文件List.c
2.2.1链表的初始化、销毁和打印
2.2.1.1初始化
函数实现:
cpp
LTNode* buyNode(LTDataType x)
{
LTNode* node = (LTNode*)malloc(sizeof(LTNode));
if (node == NULL)
{
perror("malloc fail!\n");
exit(1);
}
node->val = x;
//初始化时,不可以将next和prev指针指向NULL,因为这样不循环
node->next = node->prev = node;
return node;
}
//void LTInit(LTNode** pphead)
//{
// //初始化就是申请一个自循环的哨兵位节点
// //哨兵位节点不能被删除,节点的地址也不能发生改变
// //故后面增删改查的都不修改哨兵位,所以只用传一级指针即可
// *pphead = buyNode(-1);
//}
LTNode* LTInit() //用这种方法初始化不用传二级指针,可以保持函数接口参数一致
{
//初始化就是申请一个自循环的哨兵位节点
//哨兵位节点不能被删除,节点的地址也不能发生改变
//故后面增删改查的都不修改哨兵位,所以只用传一级指针即可
return buyNode(-1);
}
测试代码:
cpp
#include "List.h"
void test()
{
LTNode* plist = LTInit();
}
int main()
{
test();
return 0;
}
生成一个自循环的"哨兵位",因为哨兵位的值无效,所以给个初始值-1.
2.2.1.2销毁
函数实现:
cpp
//销毁
void LTDestroy(LTNode* phead)
{
assert(phead);
LTNode* pcur = phead->next;
while (pcur != phead)
{
LTNode* next = pcur->next;
free(pcur);
pcur = next;
}
//此时pcur指向phead。phead还没有被销毁
free(phead);
phead = NULL;//因为是传递的一级指针,所以这里将pos形参置为空不影响实参
//如果需要修改实参需要在函数外部修改
}
测试代码:
cpp
#include "List.h"
void test()
{
LTNode* plist = LTInit(); //初始化
LTPushBack(plist, 1);//尾插
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTDestroy(plist);
plist = NULL;
}
int main()
{
test();
return 0;
}
通过调试,销毁前:
销毁后:
销毁后再手动将plist指针置为NULL:
2.2.1.3打印
函数实现:
cpp
//打印
void LTPrint(LTNode* phead)
{
LTNode* pcur = phead->next;
while (pcur != phead)
{
printf("%d->", pcur->val);
pcur = pcur->next;
}
printf("\n");
}
测试代码:
cpp
#include "List.h"
void test()
{
LTNode* plist = LTInit(); //初始化
LTPushBack(plist, 1);//尾插
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTPrint(plist);
}
int main()
{
test();
return 0;
}
2.2.2头部/尾部插入删除
2.2.2.1尾插
函数实现:
cpp
//尾插
void LTPushBack(LTNode* phead, LTDataType x)
{
assert(phead); //哨兵位不能为空
LTNode* newnode = buyNode(x);
newnode->next = phead;
newnode->prev = phead->prev;
phead->prev->next = newnode;
phead->prev = newnode;
}
测试代码:
cpp
#include "List.h"
void test()
{
LTNode* plist = LTInit(); //初始化
LTPushBack(plist, 1);//尾插
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
int main()
{
test();
return 0;
}
2.2.2.2 尾删
函数实现:
cpp
//尾删
void LTPopBack(LTNode* phead)
{
assert(phead && phead->next != phead); //第二个断言判断链表不能为空
LTNode* del = phead->prev;
del->prev->next = phead;
phead->prev = del->prev;
free(del);
del = NULL;
}
测试代码:
cpp
#include "List.h"
void test()
{
LTNode* plist = LTInit(); //初始化
LTPushBack(plist, 1);//尾插
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTPopBack(plist); //尾删
LTPopBack(plist);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
int main()
{
test();
return 0;
}
2.2.2.3 头插
函数实现:
cpp
//头插
void LTPushFront(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* newnode = buyNode(x);
newnode->next = phead->next;
newnode->prev = phead;
phead->next->prev = newnode;
phead->next = newnode;
}
测试代码:
cpp
#include "List.h"
void test()
{
LTNode* plist = LTInit(); //初始化
LTPushBack(plist, 1);//尾插
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTPushFront(plist, 5); //头插
LTPushFront(plist, 6);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
int main()
{
test();
return 0;
}
2.2.2.4头删
函数实现:
cpp
//头删
void LTPopFront(LTNode* phead)
{
assert(phead && phead->next != phead);
LTNode* del = phead->next;
del->next->prev = phead;
phead->next = del->next;
free(del);
del = NULL;
}
测试代码:
cpp
#include "List.h"
void test()
{
LTNode* plist = LTInit(); //初始化
LTPushBack(plist, 1);//尾插
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTPopFront(plist); //头删
LTPopFront(plist);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
int main()
{
test();
return 0;
}
2.2.3查找数据
函数实现:
cpp
//查找
LTNode* LTFind(LTNode* phead, LTDataType x)
{
LTNode* pcur = phead->next;
while (pcur != phead)
{
if (pcur->val == x)
{
return pcur;
}
pcur = pcur->next;
}
return NULL;
}
测试代码:
cpp
#include "List.h"
void test()
{
LTNode* plist = LTInit(); //初始化
LTPushBack(plist, 1);//尾插
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTPrint(plist);
LTNode* pos = LTFind(plist, 1); //查找
printf("%d ", pos->val);
LTDestroy(plist);
plist = NULL;
}
int main()
{
test();
return 0;
}
2.2.4指定位置的插入删除
2.2.4.1在pos位置之后插入数据
函数实现:
cpp
//在pos位置之后插入数据
void LTInsert(LTNode* pos, LTDataType x)
{
assert(pos);
LTNode* newnode = buyNode(x);
newnode->next = pos->next;
newnode->prev = pos;
pos->next->prev = newnode;
pos->next = newnode;
}
测试代码:
cpp
#include "List.h"
void test()
{
LTNode* plist = LTInit(); //初始化
LTPushBack(plist, 1);//尾插
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTNode* pos = LTFind(plist, 1); //查找
LTInsert(pos, 7); //在指定位置之后插入数据
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
int main()
{
test();
return 0;
}
2.2.4.2 删除pos位置的数据
函数实现:
cpp
//删除pos位置的数据
void LTErase(LTNode* pos)
{
//pos理论上来说不能为phead,但是没有参数phead,无法增加校验
assert(pos);
pos->next->prev = pos->prev;
pos->prev->next = pos->next;
free(pos);
pos = NULL; //因为是传递的一级指针,所以这里将pos形参置为空不影响实参
//如果需要修改实参需要在函数外部修改
}
测试代码:
cpp
#include "List.h"
void test()
{
LTNode* plist = LTInit(); //初始化
LTPushBack(plist, 1);//尾插
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTNode* pos = LTFind(plist, 1); //查找
LTErase(pos); //删除pos位置的数据
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
int main()
{
test();
return 0;
}
2.3参考代码
cpp
//List.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType val;
struct ListNode* prev;
struct ListNode* next;
}LTNode;
//双向链表为空时,只有一个头节点
//初始化
//void LTInit(LTNode** pphead);
LTNode* LTInit();
//销毁
void LTDestroy(LTNode* phead);
//打印
void LTPrint(LTNode* phead);
//判空
bool LTEmpty(LTNode* phead);
//尾插
void LTPushBack(LTNode* phead, LTDataType x);
//尾删
void LTPopBack(LTNode* phead);
//头插
void LTPushFront(LTNode* phead, LTDataType x);
//头删
void LTPopFront(LTNode* phead);
//在pos位置之后插入数据
void LTInsert(LTNode* pos, LTDataType x);
//删除pos位置的数据
void LTErase(LTNode* pos);
//查找
LTNode* LTFind(LTNode* phead, LTDataType x);
cpp
//List.c
#include "List.h"
LTNode* buyNode(LTDataType x)
{
LTNode* node = (LTNode*)malloc(sizeof(LTNode));
if (node == NULL)
{
perror("malloc fail!\n");
exit(1);
}
node->val = x;
//初始化时,不可以将next和prev指针指向NULL,因为这样不循环
node->next = node->prev = node;
return node;
}
//void LTInit(LTNode** pphead)
//{
// //初始化就是申请一个自循环的哨兵位节点
// //哨兵位节点不能被删除,节点的地址也不能发生改变
// //故后面增删改查的都不修改哨兵位,所以只用传一级指针即可
// *pphead = buyNode(-1);
//}
//初始化
LTNode* LTInit() //用这种初始化方法可以保持函数接口参数一致
{
//初始化就是申请一个自循环的哨兵位节点
//哨兵位节点不能被删除,节点的地址也不能发生改变
//故后面增删改查的都不修改哨兵位,所以只用传一级指针即可
return buyNode(-1);
}
//销毁
void LTDestroy(LTNode* phead)
{
assert(phead);
LTNode* pcur = phead->next;
while (pcur != phead)
{
LTNode* next = pcur->next;
free(pcur);
pcur = next;
}
//此时pcur指向phead。phead还没有被销毁
free(phead);
phead = NULL;//因为是传递的一级指针,所以这里将pos形参置为空不影响实参
//如果需要修改实参需要在函数外部修改
}
//打印
void LTPrint(LTNode* phead)
{
LTNode* pcur = phead->next;
while (pcur != phead)
{
printf("%d->", pcur->val);
pcur = pcur->next;
}
printf("\n");
}
//判空
bool LTEmpty(LTNode* phead)
{
if (phead->next = phead)
{
return true;
}
return false;
}
//尾插
void LTPushBack(LTNode* phead, LTDataType x)
{
assert(phead); //哨兵位不能为空
LTNode* newnode = buyNode(x);
newnode->next = phead;
newnode->prev = phead->prev;
phead->prev->next = newnode;
phead->prev = newnode;
}
//尾删
void LTPopBack(LTNode* phead)
{
assert(phead && phead->next != phead); //第二个断言判断链表不能为空
LTNode* del = phead->prev;
del->prev->next = phead;
phead->prev = del->prev;
free(del);
del = NULL;
}
//头插
void LTPushFront(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* newnode = buyNode(x);
newnode->next = phead->next;
newnode->prev = phead;
phead->next->prev = newnode;
phead->next = newnode;
}
//头删
void LTPopFront(LTNode* phead)
{
assert(phead && phead->next != phead);
LTNode* del = phead->next;
del->next->prev = phead;
phead->next = del->next;
free(del);
del = NULL;
}
//在pos位置之后插入数据
void LTInsert(LTNode* pos, LTDataType x)
{
assert(pos);
LTNode* newnode = buyNode(x);
newnode->next = pos->next;
newnode->prev = pos;
pos->next->prev = newnode;
pos->next = newnode;
}
//删除pos位置的数据
void LTErase(LTNode* pos)
{
//pos理论上来说不能为phead,但是没有参数phead,无法增加校验
assert(pos);
pos->next->prev = pos->prev;
pos->prev->next = pos->next;
free(pos);
pos = NULL; //因为是传递的一级指针,所以这里将pos形参置为空不影响实参
//如果需要修改实参需要在函数外部修改
}
//查找
LTNode* LTFind(LTNode* phead, LTDataType x)
{
LTNode* pcur = phead->next;
while (pcur != phead)
{
if (pcur->val == x)
{
return pcur;
}
pcur = pcur->next;
}
return NULL;
}
cpp
//test.c
#include "List.h"
void test()
{
LTNode* plist = LTInit(); //初始化
LTPushBack(plist, 1);//尾插
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
//LTPopBack(plist); //尾删
//LTPopBack(plist);
//LTPushFront(plist, 5); //头插
//LTPushFront(plist, 6);
//LTPopFront(plist); //头删
//LTPopFront(plist);
LTNode* pos = LTFind(plist, 1); //查找
//LTInsert(pos, 7); //在指定位置之后插入数据
LTErase(pos); //删除pos位置的数据
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
int main()
{
test();
return 0;
}