目录
1. 链表
1.1 带头双向循环链表
双向带头循环链表(Doubly Linked Circular List)是一种特殊的链表结构,其中每个节点都包含两个指针:一个指向前一个节点(prev),另一个指向后一个节点(next)。与普通双向链表不同,双向带头循环链表的第一个节点(通常称为头节点)的 prev 指针指向链表的最后一个节点,而最后一个节点的 next 指针则指向头节点,形成一个闭环。头节点不存储数据,只用于方便操作。
以下是双向带头循环链表的一些主要特点:
- 循环性:链表的最后一个节点的 next 指针指向头节点,头节点的 prev 指针指向最后一个节点,形成一个闭环。
- 带头节点:链表包含一个不存储数据的头节点,其 prev 指针指向最后一个节点,next 指针指向第一个数据节点。头节点的存在可以简化一些操作,如插入和删除节点。
- 双向性:每个节点都有两个指针,一个指向前一个节点,另一个指向后一个节点。这使得在链表中向前和向后遍历都非常容易。
双向带头循环链表的基本操作包括:
- 初始化:创建一个头节点,并设置其 prev 和 next 指针都指向自己。
- 插入节点:在指定位置插入新节点,并更新相邻节点的指针。
- 删除节点:删除指定节点,并更新相邻节点的指针。
- 遍历:从头节点开始,可以向前或向后遍历整个链表。
这种链表结构常用于需要频繁在链表中进行插入和删除操作的场景,因为双向结构使得查找和更新节点的前后指针都非常高效。而循环性则避免了处理边界条件的复杂性。头节点的存在进一步简化了这些操作。
2. 链表的实现
2.1 结构体
首先要先把结构体定义出来,因为是带头双向循环链表,所以要有两个指针域一个指向头,一个指向尾,还要有一个数据域。
typedef int LTDataType;
typedef struct ListNode
{
struct ListNode* prev;//头指针
struct ListNode* next;//尾指针
LTDataType data;//数据域
}LTNode;
2.2 初始化
初始化链表,防止都是随机值。
//初始化
LTNode* LTInit()
{
LTNode* phead = BuyLTNode(-1);
phead->prev = phead;
phead->next = phead;
return phead;
}
2.3 打印
这个就得注意点了,只能是cur != phead不能是cur->next != phead,这样的话链表的最后一个就不能打印出来,少了一个。
//打印
void LTPrint(LTNode* phead)
{
assert(phead);
printf("哨兵位<==>");
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d<==>", cur->data);
cur = cur->next;
}
printf("\n");
}
2.4 判断空不能删
当只有头节点的时候再删就出问题了。
//判断空不能删
bool LTEmpty(LTNode* phead)
{
assert(phead);
return phead->next == phead;
}
2.5 尾插
只有找到phead->prev就是尾,所以实现起来也比较简单,只要改变4个指针的指向就可以了。
//尾插
void LTPushBack(LTNode* phead, LTDataType x)
{
assert(phead);
/*LTNode* tail = phead->prev;
LTNode* newnode = BuyLTNode(x);
tail->next = newnode;
phead->prev = newnode;
newnode->next = phead;
newnode->prev = newnode;*/
LTInsert(phead->prev, x);
}
测试代码:
//尾插测试
void TestList1()
{
LTNode* plist = LTInit();
LTPushBack(plist, 1);
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
效果展示:
2.6 头插
因为是带哨兵位的,要找到phead->next这就是我们要头插的位置。
//头插
void LTPushFront(LTNode* phead, LTDataType x)
{
assert(phead);
/*LTNode* tail = phead->next;
LTNode* newnode = BuyLTNode(x);
phead->next = newnode;
newnode->prev = phead;
newnode->next = tail;
tail->prev = newnode;*/
LTInsert(phead->next, x);
}
测试代码:
//头插测试
void TestList2()
{
LTNode* plist = LTInit();
LTPushFront(plist, 1);
LTPushFront(plist, 2);
LTPushFront(plist, 3);
LTPushFront(plist, 4);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
效果展示:
2.7 尾删
跟尾插是一样的,改4个指针的指向,再free掉。
//尾删
void LTPopBack(LTNode* phead)
{
assert(phead);
assert(!LTEmpty(phead));
/*LTNode* cur = phead->prev;
LTNode* tail = cur->prev;
free(cur);
tail->next = phead;
phead->prev = tail;*/
LTErase(phead->next);
}
测试代码:
//尾删测试
void TestList3()
{
LTNode* plist = LTInit();
LTPushBack(plist, 1);
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTPrint(plist);
LTPopFront(plist);
LTPrint(plist);
LTPopFront(plist);
LTPrint(plist);
LTPopFront(plist);
LTPrint(plist);
LTPopFront(plist);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
效果展示:
2.8 头删
哨兵位的next就是当前的头,改指针的指向。
//头删
void LTPopFront(LTNode* phead)
{
assert(phead);
assert(!LTEmpty(phead));
/*LTNode* next = phead->next;
LTNode* tail = next->next;
free(next);
phead->next = tail;
tail->prev = phead;*/
LTErase(phead->prev);
}
测试代码:
//头删测试
void TestList4()
{
LTNode* plist = LTInit();
LTPushFront(plist, 1);
LTPushFront(plist, 2);
LTPushFront(plist, 3);
LTPushFront(plist, 4);
LTPrint(plist);
LTPopBack(plist);
LTPrint(plist);
LTPopBack(plist);
LTPrint(plist);
LTPopBack(plist);
LTPrint(plist);
LTPopBack(plist);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
效果展示:
2.9 查找
尽量不要动头指针,最好是用一个指针记录下来,不然找不到头指针,再去遍历。
//查找
LTNode* LTFind(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
2.10 在pos之前插入
//在pos之前插入
void LTInsert(LTNode* pos, LTDataType x)
{
assert(pos);
LTNode* prev = pos->prev;
LTNode* newnode = BuyLTNode(x);
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
3之前插入40测试代码:
void TestList8()
{
LTNode* plist = LTInit();
LTPushFront(plist, 1);
LTPushFront(plist, 2);
LTPushFront(plist, 3);
LTPushFront(plist, 4);
LTPrint(plist);
LTNode* pos = LTFind(plist, 3);
if (pos)
{
LTInsert(pos, 30);
}
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
效果展示:
2.11 删除pos位置的值
//删除pos位置的值
void LTErase(LTNode* pos)
{
assert(pos);
LTNode* posPrev = pos->prev;
LTNode* posNext = pos->next;
free(pos);
posPrev->next = posNext;
posNext->prev = posPrev;
}
删除3位置测试代码:
void TestList9()
{
LTNode* plist = LTInit();
LTPushFront(plist, 1);
LTPushFront(plist, 2);
LTPushFront(plist, 3);
LTPushFront(plist, 4);
LTPrint(plist);
LTNode* pos = LTFind(plist, 3);
if (pos)
{
LTErase(pos);
}
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
效果展示:
2. 12 销毁
//销毁
void LTDestroy(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
}
2.13 创建节点
写一个函数创建节点,不然每次要创建节点的时候都得写一个,现在直接调用就可以。
//创建节点
LTNode* BuyLTNode(LTDataType x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
if (newnode == NULL)
{
perror("BuyLTNode");
return NULL;
}
newnode->prev = NULL;
newnode->next = NULL;
newnode->data = x;
return newnode;
}
3. test主函数
#define _CRT_SECURE_NO_WARNINGS 1
#include "List.h"
//尾插测试
void TestList1()
{
LTNode* plist = LTInit();
LTPushBack(plist, 1);
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
//头插测试
void TestList2()
{
LTNode* plist = LTInit();
LTPushFront(plist, 1);
LTPushFront(plist, 2);
LTPushFront(plist, 3);
LTPushFront(plist, 4);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
//尾删测试
void TestList3()
{
LTNode* plist = LTInit();
LTPushBack(plist, 1);
LTPushBack(plist, 2);
LTPushBack(plist, 3);
LTPushBack(plist, 4);
LTPrint(plist);
LTPopFront(plist);
LTPrint(plist);
LTPopFront(plist);
LTPrint(plist);
LTPopFront(plist);
LTPrint(plist);
LTPopFront(plist);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
//头删测试
void TestList4()
{
LTNode* plist = LTInit();
LTPushFront(plist, 1);
LTPushFront(plist, 2);
LTPushFront(plist, 3);
LTPushFront(plist, 4);
LTPrint(plist);
LTPopBack(plist);
LTPrint(plist);
LTPopBack(plist);
LTPrint(plist);
LTPopBack(plist);
LTPrint(plist);
LTPopBack(plist);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
void TestList8()
{
LTNode* plist = LTInit();
LTPushFront(plist, 1);
LTPushFront(plist, 2);
LTPushFront(plist, 3);
LTPushFront(plist, 4);
LTPrint(plist);
LTNode* pos = LTFind(plist, 3);
if (pos)
{
LTInsert(pos, 30);
}
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
void TestList9()
{
LTNode* plist = LTInit();
LTPushFront(plist, 1);
LTPushFront(plist, 2);
LTPushFront(plist, 3);
LTPushFront(plist, 4);
LTPrint(plist);
LTNode* pos = LTFind(plist, 3);
if (pos)
{
LTErase(pos);
}
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
int main()
{
TestList9();
return 0;
}
4. List.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "List.h"
//声明
LTNode* BuyLTNode(LTDataType x);
//初始化
LTNode* LTInit()
{
LTNode* phead = BuyLTNode(-1);
phead->prev = phead;
phead->next = phead;
return phead;
}
//打印
void LTPrint(LTNode* phead)
{
assert(phead);
printf("哨兵位<==>");
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d<==>", cur->data);
cur = cur->next;
}
printf("\n");
}
//判断空不能删
bool LTEmpty(LTNode* phead)
{
assert(phead);
return phead->next == phead;
}
//创建节点
LTNode* BuyLTNode(LTDataType x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
if (newnode == NULL)
{
perror("BuyLTNode");
return NULL;
}
newnode->prev = NULL;
newnode->next = NULL;
newnode->data = x;
return newnode;
}
//尾插
void LTPushBack(LTNode* phead, LTDataType x)
{
assert(phead);
/*LTNode* tail = phead->prev;
LTNode* newnode = BuyLTNode(x);
tail->next = newnode;
phead->prev = newnode;
newnode->next = phead;
newnode->prev = newnode;*/
LTInsert(phead, x);
}
//头插
void LTPushFront(LTNode* phead, LTDataType x)
{
assert(phead);
/*LTNode* tail = phead->next;
LTNode* newnode = BuyLTNode(x);
phead->next = newnode;
newnode->prev = phead;
newnode->next = tail;
tail->prev = newnode;*/
LTInsert(phead->next, x);
}
//尾删
void LTPopBack(LTNode* phead)
{
assert(phead);
assert(!LTEmpty(phead));
/*LTNode* cur = phead->prev;
LTNode* tail = cur->prev;
free(cur);
tail->next = phead;
phead->prev = tail;*/
LTErase(phead->next);
}
//头删
void LTPopFront(LTNode* phead)
{
assert(phead);
assert(!LTEmpty(phead));
/*LTNode* next = phead->next;
LTNode* tail = next->next;
free(next);
phead->next = tail;
tail->prev = phead;*/
LTErase(phead->prev);
}
//查找
LTNode* LTFind(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//在pos之前插入
void LTInsert(LTNode* pos, LTDataType x)
{
assert(pos);
LTNode* prev = pos->prev;
LTNode* newnode = BuyLTNode(x);
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
//删除pos位置的值
void LTErase(LTNode* pos)
{
assert(pos);
LTNode* posPrev = pos->prev;
LTNode* posNext = pos->next;
free(pos);
posPrev->next = posNext;
posNext->prev = posPrev;
}
//销毁
void LTDestroy(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
}
5. List.h文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int LTDataType;
typedef struct ListNode
{
struct ListNode* prev;
struct ListNode* next;
LTDataType data;
}LTNode;
//初始化
LTNode* LTInit();
//打印
void LTPrint(LTNode* phead);
//判断空不能删
bool LTEmpty(LTNode* phead);
//尾插
void LTPushBack(LTNode* phead, LTDataType x);
//头插
void LTPushFront(LTNode* phead, LTDataType x);
//尾删
void LTPopBack(LTNode* phead);
//头删
void LTPopFront(LTNode* phead);
//查找
LTNode* LTFind(LTNode* phead, LTDataType x);
//在pos之前插入
void LTInsert(LTNode* pos, LTDataType x);
//删除pos位置的值
void LTErase(LTNode* pos);
//销毁
void LTDestroy(LTNode* phead);
标签:带头,next,链表,LTNode,phead,双向,newnode,prev,plist
From: https://blog.csdn.net/weixin_70238476/article/details/139635200