0、前要
如果您是初步认识链表,或是不能完全“手撕”一个简单的单链表,可以看看我的上一篇有关“无头+无循环+单向+链表”的实现,再来看这一篇文章
1、“有头+有循环+双向+链表”的实现
(0)首先阐述两个节点的区别
- 头节点(head node)是链表中的第一个节点,它通常不存储实际数据,仅用于标记链表的开始位置。通过头节点,可以方便地访问链表中的其他节点,并进行添加、删除、遍历等操作
- 哨兵节点(sentinel node)是一种特殊的节点,它通常也不存储实际数据,仅用于简化链表的操作逻辑。哨兵节点除了与普通节点一样具有 val 和 next 两个成员变量以外,还具有特殊的含义。在链表中,哨兵节点的 next 指针通常指向链表中的第一个实际存储数据的节点,如果链表为空,则哨兵节点的 next 指针为空指针(NULL)
- 区别上来说,头节点是链表中的第一个节点,相当于链表的入口,而哨兵节点是一个特殊的节点,它不一定存在,并且它的作用是为了简化链表操作的复杂度,使得代码更加简洁和易于维护。同时,哨兵节点可以放在链表头部,也可以放在链表的任意位置。
(1)“有头+有循环+双向+链表”结构体
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode* next;
struct ListNode* prev;
}ListNode;
(2)申请节点空间
ListNode* BuyListNode(LTDataType x)
{
ListNode* cache = (ListNode*)malloc(sizeof(ListNode));//申请一个节点的空间
if (!cache)
{
perror("malloc fail!");
return NULL;
}
cache->data = x;
cache->next = NULL;
cache->prev = NULL;
return cache;
}
(3)初始化链表(创建一个“链表的头节点”)
ListNode* ListInit(void)
{
ListNode* cache = BuyListNode(0);
if (!cache) exit(-1);
cache->next = cache;
cache->prev = cache;
return cache;
}
(4)链表销毁
void ListDestory(ListNode* plist)
{
//1.防止空指针解引用
assert(plist);
ListNode* cur = plist->next;//循环变量
//2.先释放掉循环的链表
while (cur != plist)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
//3.再释放掉头节点
free(plist);
//plist = NULL;//没必要置,要置空就要使用二级指针,不使用的话就要让使用者置空
}
(5)链表打印
void ListPrint(ListNode* plist)
{
assert(plist);
ListNode* cur = plist->next;//得到起始结点,而非头节点
while (cur != plist)
{
printf("[%d]<->", cur->data);
cur = cur->next;
}
printf("[循环到头结点]\n");
}
(6)链表尾插
void ListPushBack(ListNode* plist, LTDataType x)
{
assert(plist);
ListNode* cache = BuyListNode(x);//申请节点空间
if (!cache) exit(-1);
plist->prev->next = cache;//找到指向尾节点的指针
cache->prev = plist->prev;
cache->next = plist;
plist->prev = cache;
}
(7)链表尾删
void ListPopBack(ListNode* plist)
{
assert(plist);//保证不会发生空指针解引用
assert(plist->next != plist);//保证链表不会只剩下头节点
//1.先找到这个尾节点,单独隔离出来这个地址
ListNode* tail = plist->prev;
//2.更新链表的新尾节点
tail->prev->next = plist;
plist->prev = tail->prev;
//3.释放被隔离的旧尾节点
free(tail);
tail = NULL;
}
(8)链表头插
void ListPushFront(ListNode* plist, LTDataType x)
{
assert(plist);
ListNode* cache = BuyListNode(x);//申请节点空间
if (!cache) exit(-1);
ListNode* p = plist->next;
plist->next = cache;
cache->next = p;
cache->prev = plist;
p->prev = cache;
}
(9)链表头删
void ListPopFront(ListNode* plist)
{
assert(plist);//保证不会发生空指针解引用
assert(plist->next != plist);//保证链表不会只剩下头节点
ListNode* first = plist->next;//先找到这个首节点,单独隔离出来
plist->next = first->next;
first->prev = plist;
free(first);
first = NULL;
}
(10)链表查找(返回找到的数据对应结构体的地址)
ListNode* ListFind(ListNode* plist, LTDataType x)
{
assert(plist);//避免解引用空指针
assert(plist->next != plist);//避免查找空链表
ListNode* cur = plist->next;//找到第一个节点,而非头结点
while (cur != plist)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
(11)链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);//避免解引用空指针
ListNode* cache = BuyListNode(x);//购买新节点
ListNode* p = pos->prev;
pos->prev = cache;
cache->next = pos;
cache->prev = p;
p->next = cache;
}
(12)链表删除pos位置的结点
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
pos = NULL;
}
(13)测试用例
void test()
{
ListNode* pl = ListInit();//创建一个头节点
//根据这个头结点进行“增删查改”
ListPushBack(pl, 1);//尾插
ListPushBack(pl, 2);//尾插
ListPushBack(pl, 3);//尾插
ListPushBack(pl, 4);//尾插
ListPrint(pl);//打印链表
ListPopBack(pl);//尾删
ListPopBack(pl);//尾删
ListPrint(pl);//打印链表
ListPopBack(pl);//尾删
ListPrint(pl);//打印链表
ListPopBack(pl);//尾删
ListPrint(pl);//打印链表
//ListPopFront(pl);//尾删(非法)
//ListPrint(pl);//打印链表
ListPushBack(pl, 1);//尾插
ListPushBack(pl, 2);//尾插
ListPushBack(pl, 3);//尾插
ListPushBack(pl, 4);//尾插
ListPopBack(pl);//尾删
ListPrint(pl);//打印链表
printf("%p\n", ListFind(pl, 1));//查找数据为1的节点,并且打印地址
printf("%d\n", ListFind(pl, 1)->data);//查找数据为1的节点,并且打印出指针指向的数据
printf("%p\n", ListFind(pl, 2));//查找数据为2的节点,并且打印地址
printf("%d\n", ListFind(pl, 2)->data);//查找数据为2的节点,并且打印出指针指向的数据
printf("%p\n", ListFind(pl, 3));//查找数据为3的节点,并且打印地址
printf("%d\n", ListFind(pl, 3)->data);//查找数据为3的节点,并且打印出指针指向的数据
printf("%p\n", ListFind(pl, 0));//查找数据为1的节点,并且打印地址
ListPushFront(pl, 0);//头插
ListPushFront(pl, -1);//头插
ListPushFront(pl, -2);//头插
ListPopBack(pl);//尾删
ListPopBack(pl);//尾删
ListPopBack(pl);//尾删
ListPrint(pl);//打印链表
ListPopBack(pl);//尾删
ListPopBack(pl);//尾删
ListPopBack(pl);//尾删
ListPushFront(pl, -2);//头插
ListPrint(pl);//打印链表
ListPopBack(pl);//尾删
ListPushFront(pl, 10);//头插
ListPushFront(pl, 100);//头插
ListPushFront(pl, 1000);//头插
ListPrint(pl);//打印链表
ListPopFront(pl);//头删
ListPopFront(pl);//头删
ListPopFront(pl);//头删
ListPrint(pl);//打印链表
//ListPopFront(pl);//头删(非法)
//ListPrint(pl);//打印链表
ListPushFront(pl, 10);//头插
ListPushFront(pl, 100);//头插
ListPushFront(pl, 1000);//头插
ListPrint(pl);//打印链表
ListInsert(ListFind(pl, 10), -1);//任意插入
ListInsert(ListFind(pl, 100), -1);//任意插入
ListInsert(ListFind(pl, 1000), -1);//任意插入
ListPrint(pl);//打印链表
//ListInsert(ListFind(pl, 1), -1);//任意插入(非法)
//ListPrint(pl);//打印链表
ListPopFront(pl);//头删
ListPopFront(pl);//头删
ListPopFront(pl);//头删
ListPopFront(pl);//头删
ListPopFront(pl);//头删
ListPopFront(pl);//头删
ListPushFront(pl, 10);//头插
ListPrint(pl);//打印链表
ListInsert(ListFind(pl, 10), -1);//任意插入
ListPrint(pl);//打印链表
ListPopFront(pl);//头删
ListPopFront(pl);//头删
ListPrint(pl);//打印链表
ListPushFront(pl, 10);//头插
ListPushFront(pl, 100);//头插
ListPushFront(pl, 1000);//头插
ListPrint(pl);//打印链表
ListErase(ListFind(pl, 10));//任意删除
ListPrint(pl);//打印链表
ListErase(ListFind(pl, 100));//任意删除
ListPrint(pl);//打印链表
ListErase(ListFind(pl, 1000));//任意删除
ListPrint(pl);//打印链表
//ListErase(ListFind(pl, 1000));//任意删除(非法)
//ListPrint(pl);//打印链表
ListDestory(pl);//销毁链表
}
2、其他类型的链表
在我的博文中我写了有关链表的两种结构,一种是结构最简单的“无头+非循环+单向+链表”,另外一种是结构最复杂的“有头+有循环+双向+链表”。在以后我还会写其他类型的链表,但是您会发现,只要掌握了”最简单“和”最复杂“的,那么其他类型的链表也都是类似的写法