IT评测·应用市场-qidao123.com
标题:
【初探数据布局】线性表——链表(二)带头双向循环链表(详解与实现)
[打印本页]
作者:
石小疯
时间:
2025-3-14 16:20
标题:
【初探数据布局】线性表——链表(二)带头双向循环链表(详解与实现)
1.
什么是带头双向循环链表?
带头双向循环链表,顾名思义,包罗以下几个重要特点:
双向性
:每个节点不仅有指向下一个节点的next指针,还有一个指向前一个节点的prev指针。这使得遍历链表时可以从任意一个节点开始,向前或向后都可以。
循环性
:链表的最后一个节点的next指针指向头节点,而头节点的prev指针指向最后一个节点,形成一个闭环。即链表没有尾节点,头节点和尾节点毗连起来,方便链表的尾插尾删等操作。
头节点(哨兵节点)
:链表通过引入一个特别的头节点(哨兵节点)来简化插入和删除操作。这个头节点自己不存储现实数据,只是作为一个占位符,避免了空链表或单一节点链表的特别情况。(我的上一篇文章解说过,欢迎来学习哦【初探数据布局】链表OJ算法——哨兵位(归并两个有序链表详解))
2.
带头双向循环链表的布局
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data; // 存储节点数据
struct ListNode* next; // 指向下一个节点
struct ListNode* prev; // 指向前一个节点
} ListNode;
复制代码
每个ListNode布局体包罗:
data:保存节点的数据。
next:指向下一个节点。
prev:指向前一个节点。
3.
带头双向循环链表的操作
带头双向循环链表支持常见的增删查改操作,以下是常用操作的实现。
3.1
初始化链表
ListNode* ListInit()
{
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
if (head == NULL) {
perror("malloc fail");
exit(-1);
}
head->next = head; // 头节点的next指向自己,形成循环
head->prev = head; // 头节点的prev也指向自己,形成循环
return head;
}
复制代码
在初始化时,创建一个头节点,并将其next和prev指针都指向自身,这样链表初始时是空的,而且形成了一个循环布局。
3.2
插入节点
尾插操作
:在链表尾部插入新节点。
void ListPushBack(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListInsert(pHead, x);
}
复制代码
ListInsert用于执行具体的插入操作,它将新节点插入到链表末尾。
头插操作
:在链表头部插入新节点。
void ListPushFront(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListInsert(pHead->next, x);
}
复制代码
3.3
删除节点
尾删操作
:删除链表尾部的节点。
void ListPopBack(ListNode* pHead)
{
assert(pHead);
ListNode* tail = pHead->prev;
ListErase(tail);
}
复制代码
头删操作
:删除链表头部的节点。
void ListPopFront(ListNode* pHead)
{
assert(pHead);
ListErase(pHead->next);
}
复制代码
3.4
查找节点
通过值查找链表中的节点。
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* cur = pHead->next;
while (cur != pHead) {
if (cur->data == x)
return cur;
cur = cur->next;
}
return NULL;
}
复制代码
3.5
删除指定节点
通过pos指针删除指定位置的节点。
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* after = pos->next;
ListNode* behind = pos->prev;
behind->next = pos->next;
after->prev = pos->prev;
free(pos);
pos = NULL;
}
复制代码
4.
带头双向循环链表的上风
简化链表操作
:带头节点和双向指针的联合使得在链表的任意位置插入和删除操作更加简便。特别是通过哨兵节点,可以避免单独处理空链表、头节点和尾节点的特别情况。
双向遍历
:通过prev和next指针,我们可以实现从链表任意节点出发,向前或向后遍历。这为一些复杂操作提供了极大的灵活性。
内存管理
:链表在操作时可以动态地分配和开释节点内存,减少内存浪费。
5.
完整代码
List.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
// 带头+双向+循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode* next;
struct ListNode* prev;
}ListNode;
// 创建返回链表的头结点.
ListNode* ListInit();
// 双向链表销毁
void ListDestory(ListNode* pHead);
// 双向链表打印
void ListPrint(ListNode* pHead);
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);
// 双向链表尾删
void ListPopBack(ListNode* pHead);
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x);
// 双向链表头删
void ListPopFront(ListNode* pHead);
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos);
复制代码
List.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"List.h"
ListNode* ListInit()
{
//设置哨兵位头结点
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
if (head == NULL) {
perror("malloc fail");
exit(-1);
}
head->next = head;
head->prev = head;
return head;
}
void ListDestory(ListNode* pHead)
{
assert(pHead);
ListNode* cur = pHead->next;
while (cur != pHead) {
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(pHead);
pHead = NULL;
}
void ListPrint(ListNode* pHead)
{
assert(pHead);
ListNode* cur = pHead->next;
printf("HEAD=>");
while (cur != pHead) {
printf("%d<=>",cur->data);
cur = cur->next;
}
printf("\n");
}
ListNode* CreateNewNode(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode == NULL) {
perror("malloc fail");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
void ListPushBack(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListInsert(pHead, x);
}
void ListPopBack(ListNode* pHead)
{
assert(pHead);
ListNode* tail = pHead->prev;
ListErase(tail);
}
void ListPushFront(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListInsert(pHead->next, x);
}
void ListPopFront(ListNode* pHead)
{
assert(pHead);
ListErase(pHead->next);
}
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* cur = pHead->next;
while (cur != pHead) {
if (cur->data == x)
return cur;
cur = cur->next;
}
return NULL;
}
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* newnode = CreateNewNode(x);
ListNode* cur = pos->prev;
pos->prev = newnode;
newnode->next = pos;
cur->next = newnode;
newnode->prev = cur;
}
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* after = pos->next;
ListNode* behind = pos->prev;
behind->next = pos->next;
after->prev = pos->prev;
free(pos);
pos = NULL;
}
复制代码
Test.c
在现实使用时,我们可以通过以下代码进行链表的操作:
void test()
{
ListNode* plist;
plist = ListInit(); // 初始化链表
// 插入元素
ListPushBack(plist, 1);
ListPushBack(plist, 2);
ListPushBack(plist, 3);
ListPushBack(plist, 4);
ListPrint(plist); // 打印链表
// 删除元素
ListPopBack(plist);
ListPopBack(plist);
ListPrint(plist); // 打印链表
// 头部插入
ListPushFront(plist, 0);
ListPrint(plist); // 打印链表
ListPopFront(plist);
ListPrint(plist); // 打印链表
// 查找元素并插入
ListNode* pos = ListFind(plist, 2);
ListInsert(pos, 20);
ListPrint(plist); // 打印链表
}
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/)
Powered by Discuz! X3.4