带头双向循环链表,顺序表和链表的比较

双向链表

单链表结点中只有一个指向其后继的指针,使得单链表只能从前往后依次遍历,要访问某个结点的前驱(插入、删除操作时),只能从头开始遍历,访问前驱的时间复杂度为O(N)。为了克服这个缺点,引入了双链表。

循环链表

循环单链表和单链表的区别在于,表中最后一个结点的指针不是NULL,而改为指向头结点,从而整个链表形成一个环,如图所示。
image.png
在循环单链表中,表尾结点d3的next域指向d1,故表中没有指针域为NULL的结点,因此,循环单链表的判空条件不是头结点的指针是否为空,而是它是否等于头指针d1。

带头双向循环链表的增删查改

带头双向循环链表

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>typedef int LTDataType;typedef struct ListNode
{LTDataType data;struct ListNode* next;struct ListNode* prev;
}ListNode;//创建返回链表的头结点
ListNode* BuyLTNode(LTDataType x);//初始化双向链表
ListNode* ListInit();//销毁双向链表
void ListDestory(ListNode* phead);//打印双向链表
void ListPrint(ListNode* phead);//判断链表是否为空
bool LTEmpty(ListNode* phead);//查找双向链表
ListNode* LTFind(ListNode* phead, LTDataType x);//双向链表的头插
void ListPushFront(ListNode* phead, LTDataType x);//双向链表的尾插
void ListPushBack(ListNode* phead, LTDataType x);//双向链表的头删
void ListPopFront(ListNode* phead);//双向链表的尾删
void ListPopBack(ListNode* phead);//双向链表在pos位置之前的插入
void ListInsert(ListNode* pos, LTDataType x);//双向链表删除pos位置的结点
void ListErase(ListNode* pos);

双向链表的头插
image.png

#include  "list.h"//创建返回链表的头结点
ListNode* BuyLTNode(LTDataType x)
{ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));if (newnode == NULL){perror("newnode malloc fail");return NULL;}newnode->data = x;newnode->next = NULL;newnode->prev = NULL;return newnode;
}//初始化双向链表
ListNode* ListInit() 
{ListNode* phead = BuyLTNode(-1);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);
}//打印双向链表
void ListPrint(ListNode* phead)
{assert(phead);printf("sentinel《==》");ListNode* cur = phead->next;while (cur != phead){printf("%d《==》", cur->data);cur = cur->next;}printf("\n");
}//判断链表是否为空
bool LTEmpty(ListNode* phead)
{assert(phead);return phead->next == phead;//等于就是空
}//查找双向链表
ListNode* LTFind(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 ListPushFront(ListNode* phead, LTDataType x)
{assert(phead);/*ListInsert(phead->next,x);//使用 Insert 进行头插*/ListNode* newnode = BuyLTNode(x);ListNode* frist = phead->next;phead->next = newnode;newnode->prev = phead;newnode->next = frist;frist->prev = newnode;/*newnode->next = phead->next;phead->next->prev = newnode;newnode->prev = phead;phead->next = newnode;*/
}//双向链表的尾插
void ListPushBack(ListNode* phead, LTDataType x)
{assert(phead);/*ListInsert(phead->prev,x);//使用 Insert 进行尾插*/ListNode* newnode = BuyLTNode(x);ListNode* tail = phead->prev;newnode->next = phead;phead->prev = newnode;newnode->prev = tail;tail->next = newnode;
}//双向链表的头删
void ListPopFront(ListNode* phead) 
{assert(phead);assert(!LTEmpty(phead));//断言条件尽量不写在一起,不然无法判断是其中哪个条件出错/*ListErase(phead->next);//使用 Erase 进行头删*/ListNode* frist = phead->next;ListNode* fristnext = frist->next;//增加代码的可读性free(frist);phead->next = fristnext;fristnext->prev = phead;/*if (frist != phead){frist->next->prev = phead;phead->next = frist->next;free(frist);}*/
}//双向链表的尾删
void ListPopBack(ListNode* phead)
{assert(phead);assert(!LTEmpty(phead));/*ListErase(phead->prev);//使用 Erase 进行尾删*/ListNode* tail = phead->prev;ListNode* tailprev = tail->prev;free(tail);tailprev->next = phead;phead->prev = tailprev;/*if (tail != phead){phead->prev = tail->prev;tail->prev->next = phead;free(tail);}*/
}//双向链表在pos位置之前的插入
void ListInsert(ListNode* pos, LTDataType x) {assert(pos);ListNode* prev = pos->prev;ListNode* newnode = BuyLTNode(x);prev->next = newnode;newnode->prev = prev;newnode->next = pos;pos->prev = newnode;
}//双向链表删除pos位置的结点
void ListErase(ListNode* pos) {assert(pos);//注意,当pos传递为phead的值时,即哨兵位值时,会出现错误。//可将phead传递进来,使pos!=phead,,,否则就干脆不判断,出现错误时再解决ListNode* prev = pos->prev;ListNode* next = pos->next;prev->next = next;next->prev = prev;free(pos);
}
#include  "list.h"void test1()
{ListNode* plist = ListInit();ListPushFront(plist, 1);ListPushFront(plist, 1);ListPushFront(plist, 2);ListPushFront(plist, 2);ListPushFront(plist, 3);ListPushFront(plist, 3);ListPushBack(plist, 11);ListPushBack(plist, 22);ListPrint(plist);ListPopFront(plist);ListPopBack(plist);ListPrint(plist);ListNode* pos = LTFind(plist,3);ListInsert(pos, 33);pos = LTFind(plist, 1);ListErase(pos);ListPrint(plist);ListDestory(plist);plist = NULL;
}int main()
{test1();return 0;
}

image.png

顺序表和链表比较

顺序表的优点:

1.尾插尾删效率不错,
2.下标的随机访问
3.CPU高速缓存命中率会更高(物理空间时连续的)

顺序表的缺点:

1.前面部分插入删除数据,效率低O(N),需要依次挪动数据,物理空间是连续的。
2.连续的物理空间,空间不够,需要扩容。
a.扩容是需要付出代价的
b.一般伴随着空间浪费

链表的优点:

1.任意位置插入删除,O(1)。ex:带头双向循环链表
2.按需申请空间

链表的缺点:

1.不支持下标的随机访问
2.CPU高速缓存命中率会更低

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/593688.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

权限认证SpringCloud GateWay、SpringSecurity、OAuth2.0、JWT一网打尽!

权限认证SpringCloud GateWay、SpringSecurity、OAuth2.0、JWT一网打尽 一、SpringCloud GateWay 1.它是如何工作的&#xff1f; ​ 客户端向 Spring Cloud Gateway 发出请求。如果Gateway处理程序映射确定一个请求与路由相匹配&#xff0c;它将被发送到Gateway Web处理程序。…

产品推荐 | 中科亿海微推出亿迅®A8000金融FPGA加速卡

01、产品概述 亿迅A8000金融加速卡&#xff0c;是中科亿海微联合金融证券领域的战略合作伙伴北京睿智融科&#xff0c;将可编程逻辑芯片与金融行业深度结合&#xff0c;通过可编程逻辑芯片对交易行情加速解码&#xff0c;实现低至纳秒级的解码引擎&#xff0c;端到端的处理时延…

C语言 | Leetcode C语言题解之第10题正则表达式匹配

题目&#xff1a; 题解&#xff1a; bool isMatch(char* s, char* p) {int m strlen(s);int n strlen(p);// dp[i][j] 表示 s 的前 i 个字符和 p 的前 j 个字符是否匹配bool dp[m 1][n 1];memset(dp, false, sizeof(dp));dp[0][0] true; // 空字符串和空模式匹配// 处理 …

No dashboards are active for the current data set.

再次记录一下这个离谱的问题 之前出现这个问题是因为目录没写对 今天遇到这个问题的原因是目录是对的&#xff0c;跟目录是否带有中文也没关系 是writer写入的时候写的是空的&#xff0c;离谱的是写入是空的情况下也会生成events日志文件&#xff0c;看起来好像成功写入了一样&…

遭遇.locked勒索病毒?这份恢复宝典请收好

在当今数字化时代&#xff0c;网络安全问题日益严峻。其中&#xff0c;勒索病毒无疑是近年来网络安全领域的一大难题。.locked勒索病毒作为一种典型的恶意软件&#xff0c;已经给许多企业和个人带来了巨大的经济损失和数据泄露风险。本文将为您提供一份详尽的恢复宝典&#xff…

ModuleNotFoundError: No module named ‘einops‘解决办法

安装对应的库就好 pip install einops -i https://pypi.tuna.tsinghua.edu.cn/simple 拓展——在python中einops模块有什么作用 einops 是一个 Python 库&#xff0c;它提供了一种简洁、易读的方式来操作多维数组&#xff08;通常是 NumPy 数组或 PyTorch 张量&#xff09;。e…

微信小程序 ---- 慕尚花坊 代码优化

代码优化 1. 分享功能 思路分析&#xff1a; 目前小程序页面都没有配置分享功能&#xff0c;需要给小程序页面设置分享功能。 但是并不是所有页面都需要设置分享功能&#xff0c; 具体哪些页面需要设置分享功能&#xff0c;可以和产品经理进行协商。 首页商品列表商品详情…

四川易点慧电子商务抖音小店品质之选,信赖之源

随着互联网的快速发展&#xff0c;电子商务以其便捷、高效的特点成为越来越多消费者购物的首选。四川易点慧电子商务抖音小店&#xff0c;作为众多电商平台中的佼佼者&#xff0c;以其卓越的品质和优质的服务赢得了广大消费者的信赖。 一、品质保证&#xff0c;消费无忧 四川易…

【Qt】:常用控件(二:QWidget核心属性)

常用控件&#xff08;二&#xff09; 一.cursor&#xff08;光标形状&#xff09;二.font&#xff08;字体信息&#xff09;三.toolTip&#xff08;提示显示&#xff09;四.focusPolicy&#xff08;焦点&#xff09;五.styleSheet&#xff08;文本样式&#xff09; 一.cursor&a…

【注册中心】ZooKeeper

文章目录 概述Zookeeper的应用场景Zookeeper的角色Zookeeper 的数据模型zookeeper客户端常用命令Zookeeper的核心功能Zookeeper的架构与集群规则Zookeeper的工作模式Zookeeper如何实现分布式锁Zookeeper JavaAPI&#xff08;Curator&#xff09;来源 概述 Zookeeper 是一个开源…

Java笔试题总结

HashSet子类依靠()方法区分重复元素。 A toString(),equals() B clone(),equals() C hashCode(),equals() D getClass(),clone() 答案:C 解析: 先调用对象的hashcode方法将对象映射为数组下标,再通过equals来判断元素内容是否相同 以下程序执行的结果是&#xff1a; class X{…

十个排序算法

目录 冒泡排序(Bubble Sort) 选择排序(Select Sort) 插入排序&#xff08;InsertSort&#xff09; 希尔排序&#xff08;ShellSort&#xff09; 计数排序&#xff08;CountSort&#xff09; 快速排序&#xff08;QuickSort&#xff09; 归并排序&#xff08;Merge Sort&a…