網站首頁 編程語言 正文
前言
關于鏈表這一塊,寫了多篇博客,學習了順序表、單鏈表、及其一些練習題
順序表:傳送門:順序表
單鏈表:傳送門:單鏈表1?? 鏈表2
鏈表OJ:傳送門:鏈表OJ
今天,我又來水一水博客,?介紹關于雙鏈表。
帶頭雙向循環鏈表的結構
實際上,單鏈表也存在一個比較大的缺陷:
1.不能從后往前遍歷
2.無法找到前驅
?除了單鏈表之外,我們自然還有雙向鏈表,我們要說的就是帶頭雙向循環鏈表,簡單理解為:帶頭結點的,有兩個方向的。循環的。結構圖如下:
結構雖然比較復雜,但是極大方便我們找結點,比如可以直接找到尾結點,然后再進入相關的操作。實際代碼的操作將會比單鏈表簡單,極為方便,這里不做過多說明,直接上手代碼
代碼操作
我們直奔主題,進入代碼實現的操作,之前的操作如果理解了,那我相信這個對于你來說肯定是不難的。下面直接給出源碼:
List.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#include <stdbool.h>
typedef int LTDataType;
//帶頭雙向循環--最優鏈表結構,在任意位置插入刪除數據都是O(1)
typedef struct listNode
{
struct ListNode* next;
struct ListNode* prev;
LTDataType data;
}ListNode;
//初始化
ListNode*ListInit();
//銷毀
void ListDestory(ListNode* phead);
//打印
void ListPrint(ListNode* phead);
//尾插
void ListPushBack(ListNode* phead, LTDataType x);
//頭插
void ListPushFront(ListNode* phead, LTDataType x);
//頭刪
void ListPopFront(ListNode* phead);
//尾刪
void ListPopBack(ListNode* phead);
ListNode* ListFind(ListNode* phead, LTDataType x);
//在pos位置之前插入x
void ListInsert(ListNode* pos, LTDataType x);
//刪除pos位置的值
void ListErase(ListNode* pos);
List.c
#include "List.h"
//開辟一個新結點
ListNode* BuyListNode(LTDataType x)
{
ListNode* newnode =(ListNode*)malloc(sizeof(ListNode));
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
//初始化
ListNode* ListInit()
{
ListNode*phead = BuyListNode(0);
phead->next = phead;
phead->prev = phead;
return phead;
}
//銷毀
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)
{
ListNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
//尾插
void ListPushBack(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* tail = phead->prev;
ListNode* newnode = BuyListNode(x);
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
}
//頭插
void ListPushFront(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* first = phead->next;
ListNode* newnode = BuyListNode(x);
newnode->next = first;
first->prev = newnode;
phead->next = newnode;
newnode->prev = phead;
}
//頭刪
void ListPopFront(ListNode* phead)
{
assert(phead);
assert(phead->next != phead);
ListNode* first = phead->next;
ListNode* second = first->next;
phead->next = second;
second->prev = phead;
free(first);
first = NULL;
}
//尾刪
void ListPopBack(ListNode* phead)
{
assert(phead);
assert(phead->next != phead);
ListNode* tail = phead->prev;
ListNode* prev = tail->prev;
prev->next = phead;
phead->prev = prev;
free(tail);
tail = NULL;
}
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;
}
//在pos位置之前插入x
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* newnode = BuyListNode(x);
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
//刪除pos位置的值
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}
Test.c
#include "List.h"
void TestList1()
{
ListNode* plist = ListInit();
ListPushBack(plist, 1);
ListPushBack(plist, 2);
ListPushBack(plist, 3);
ListPushBack(plist, 4);
ListPrint(plist);
ListPushFront(plist, 0);
ListPushFront(plist, -1);
ListPrint(plist);
ListPopFront(plist);
ListPopFront(plist);
ListPopFront(plist);
ListPrint(plist);
ListPopBack(plist);
ListPrint(plist);
}
void TestList2()
{
ListNode* plist = ListInit();
ListPushBack(plist, 1);
ListPushBack(plist, 2);
ListPushBack(plist, 3);
ListPushBack(plist, 4);
ListPrint(plist);
ListNode* pos = ListFind(plist, 3);
if (pos)
{
pos->data *= 10;
printf("找到了,并且*10\n");
}
else
{
printf("沒找到\n");
}
ListPrint(plist);
ListInsert(pos, 300);
ListPrint(plist);
ListErase(pos);
ListPrint(plist);
}
int main()
{
TestList2();
return 0;
}
原文鏈接:https://blog.csdn.net/weixin_60478154/article/details/124092194
相關推薦
- 2022-05-26 為Jenkins添加SSH全局憑證_相關技巧
- 2023-05-08 C語言中關于樹和二叉樹的相關概念_C 語言
- 2022-03-31 C語言運算符的重載詳解_C 語言
- 2023-03-25 python?中?關于reverse()?和?reversed()的用法詳解_python
- 2022-07-16 結構體通過成員變量獲取主結構體地址(struct)
- 2022-04-14 Python實現用戶登錄注冊_python
- 2022-11-29 C#中泛型容器Stack<T>的用法并實現”撤銷/重做”功能_C#教程
- 2022-10-24 通過5個例子讓你學會Pandas中的字符串過濾_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支