当前位置: 首页 > news >正文

湘潭企业seo优化哪家好seo顾问阿亮

湘潭企业seo优化哪家好,seo顾问阿亮,html网站怎么做几个网页,南昌简单做网站大家好,今天为大家分享一下第二个数据结构——单链表 先打个广告:这里是博主写道顺序表,大家也可以查看:顺序表详解 首先: 我们学完顺序表的时候,我们发现有以下问题: 中间/头部的插入删除&…

大家好,今天为大家分享一下第二个数据结构——单链表
先打个广告:这里是博主写道顺序表,大家也可以查看:顺序表详解
首先:

我们学完顺序表的时候,我们发现有以下问题:
中间/头部的插入删除,时间复杂度为O(N)
增容需要申请新空间,拷贝数据,释放旧空间、消耗大量资源。
增容一般是2倍的增长,势必会有一定的空间浪费。例如当前容量为300,满了以后增容到600,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了295个数据空间。

链表就能够很好的解决这些问题!

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而线性表和顺序表相应的时间复杂度分别是O(logn)和O(1)。

单链表模样如下:
在这里插入图片描述

博主分享的链表接口如下:

typedef int SLTDataType;//存储的数据类型
typedef struct SListNode {SLTDataType data;struct SListNode* next;
}SLTNode;//打印链表
void SListPrint(SLTNode* phead);
//创建节点
SLTNode* BuySListNode(SLTDataType x);
//尾插
void SListPushBack(SLTNode** phead, SLTDataType x);
//头插
void SListPushFront(SLTNode** phead, SLTDataType x);
//尾删
void SListPopBack(SLTNode** phead);
//头删
void SListPopFront(SLTNode** phead);
//查找
SLTNode* SListFind(SLTNode* phead, SLTDataType x);
//在pos位置之前插入
void SListInsert(SLTNode** phead, SLTNode* pos, SLTDataType x);
//删除pos位置
void SListErase(SLTNode** phead, SLTNode* pos);
//在pos位置之后插入
void SListInsertAfter(SLTNode* pos, SLTDataType x);
//删除pos之后的位置
void SListEraseAfter(SLTNode* pos);

下面对每个接口的实现进行说明:

1、创建新节点的接口函数

SLTNode* BuySListNode(SLTDataType x)
{SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));//出这个函数newnode被销毁,但是它保存的地址空间是malloc出来的,不会销毁assert(newnode);newnode->data = x;newnode->next = NULL;return newnode;
}

assert断言检查一下,如果申请失败就报错提示,申请成功就返回创建的节点的地址

2、打印数据函数的实现

void SListPrint(SLTNode* phead)
{SLTNode* cur = phead;while (cur!= NULL){printf("%d->", cur->data);cur = cur->next;}printf("NULL\n");
}

定义一个指针cur指向头结点,使用这个指针循环遍历,这个指针指向的不为空的话就继续循环,在循环中打印cur->data,对应的操作是printf(“%d->”, cur->data);打印%d后面加->是为了方便观察;然后将cur指针移动到下一个结点的位置对应操作是cur = cur->next;,继续打印,直到cur为空打印完毕!

3、尾插函数的实现

void SListPushBack(SLTNode** phead, SLTDataType x)
{//需要传入头节点,以供寻找尾结点从而进行尾插SLTNode* newnode = BuySListNode(x);//创造新结点assert(phead);//就算链表为空,但是他的地址不能为空呀if (*phead == NULL){*phead = newnode;}else {SLTNode* tail = *phead;//循环找尾节点while (tail->next != NULL){tail = tail->next;}//找到当下最后一个节点后让它指向插入的这个节点tail->next = newnode;}
}

在这里插入图片描述

在这里插入图片描述

4、头插函数的实现

void SListPushFront(SLTNode** phead, SLTDataType x)
{assert(phead);//就算链表为空,但是他的地址不能为空呀SLTNode* newnode = BuySListNode(x);//创造新结点newnode->next = *phead;*phead = newnode;
}

将创建的新结点的地址存放在newnode指针变量中,pphead为原先头结点的地址,头插一个新结点后,应该将新结点的next存放原先头结点的地址,对应操作为newnode->next = pphead;然后保存在pphead应该为新的头结点的地址,也就是newnode所指向的地址,对应操作为pphead = newnode;

在这里插入图片描述

5、尾删函数的实现

void SListPopBack(SLTNode** phead)
{//if (*phead == NULL)//温柔的检查//{//	return;//}assert(phead);//就算链表为空,但是他的地址不能为空呀assert(*phead!=NULL);//暴力检查if ((*phead)->next == NULL)//如果只有一个节点{free(*phead);*phead = NULL;//                            要改变phead,就需要二级指针}else//如果有多个结点{SLTNode* tailPrev = *phead;SLTNode* tail = *phead;//循环找尾节点while (tail->next != NULL){tailPrev = tail;tail = tail->next;}free(tail);tailPrev->next = NULL;}}

先创建两个指针,一个tail,一个tailprev,便利一遍链表,tail保存最后一个节点,tailprev保存最后一个节点的前一个节点,free掉tail,将tailprev的next指向NULL,就完成了尾删
如果只有一个节点,则释放后要通过对二级指针解引用将其置为空
在这里插入图片描述

6、头删函数的实现

void SListPopFront(SLTNode** phead)//                    要改变phead,就需要二级指针
{assert(*phead!=NULL);//链表为空就不能删除呀assert(phead);//就算链表为空,但是他的地址不能为空呀//if (*phead == NULL)//温柔的检查//{//	return;//}SLTNode* Next = (*phead)->next;free(*phead);*phead = Next;
}

提前保存好头节点的next值,删除后将头节点变为保存的next
在这里插入图片描述

7、查找函数的实现

SLTNode* SListFind(SLTNode* phead, SLTDataType x)
{SLTNode* cur = phead;while (cur){if (cur->data == x){return cur;}cur = cur->next;}return NULL;
}

遍历一遍链表,哪个节点的数据和要求的数据相同,则返回该节点

在这里插入图片描述

8、任意位置插入节点函数的实现

void SListInsert(SLTNode** phead, SLTNode* pos, SLTDataType x)
{assert(pos);//插入位置 传 错,为空就报错assert(*phead);assert(phead);//就算链表为空,但是他的地址不能为空呀if (pos == *phead){SListPushFront(phead, x);//头插}else{SLTNode* prev = *phead;while (prev->next !=pos){prev = prev->next;}SLTNode* newnode = BuySListNode(x);prev->next = newnode;newnode->next = pos;}
}

这个函数写出来之后就可以利用他对前面所写的头插和尾插进行改写了,大佬们可以尝试一下呀

在这里插入图片描述

9、删除节点函数的实现

void SListErase(SLTNode** phead, SLTNode* pos)
{assert(pos);//插入位置 传 错,为空就报错assert(*phead);assert(phead);//就算链表为空,但是他的地址不能为空呀if (pos == *phead){SListPopFront(phead);}else{SLTNode* prev = *phead;while (prev->next!= pos){prev = prev->next;}prev->next = pos->next;free(pos);pos = NULL;//可有可无}
}

这个函数写出来之后就可以利用他对前面所写的头删和尾删进行改写了,大佬们可以尝试一下呀
在这里插入图片描述
下面是完整的代码:

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>typedef int SLTDataType;
typedef struct SListNode {SLTDataType data;struct SListNode* next;
}SLTNode;//打印链表
void SListPrint(SLTNode* phead);
//创建节点
SLTNode* BuySListNode(SLTDataType x);
//尾插
void SListPushBack(SLTNode** phead, SLTDataType x);
//头插
void SListPushFront(SLTNode** phead, SLTDataType x);
//尾删
void SListPopBack(SLTNode** phead);
//头删
void SListPopFront(SLTNode** phead);
//查找
SLTNode* SListFind(SLTNode* phead, SLTDataType x);
//在pos位置之前插入
void SListInsert(SLTNode** phead, SLTNode* pos, SLTDataType x);
//删除pos位置
void SListErase(SLTNode** phead, SLTNode* pos);
//在pos位置之后插入
void SListInsertAfter(SLTNode* pos, SLTDataType x);
//删除pos之后的位置
void SListEraseAfter(SLTNode* pos);
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"void SListPrint(SLTNode* phead)
{SLTNode* cur = phead;while (cur!= NULL){printf("%d->", cur->data);cur = cur->next;}printf("NULL\n");
}SLTNode* BuySListNode(SLTDataType x)
{SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));//出这个函数newnode被销毁,但是它保存的地址空间是malloc出来的,不会销毁assert(newnode);newnode->data = x;newnode->next = NULL;return newnode;
}void SListPushBack(SLTNode** phead, SLTDataType x)
{//需要传入头节点,以供寻找尾结点从而进行尾插SLTNode* newnode = BuySListNode(x);//创造新结点assert(phead);//就算链表为空,但是他的地址不能为空呀if (*phead == NULL){*phead = newnode;}else {SLTNode* tail = *phead;//循环找尾节点while (tail->next != NULL){tail = tail->next;}//找到当下最后一个节点后让它指向插入的这个节点tail->next = newnode;}
}void SListPushFront(SLTNode** phead, SLTDataType x)
{assert(phead);//就算链表为空,但是他的地址不能为空呀SLTNode* newnode = BuySListNode(x);//创造新结点newnode->next = *phead;*phead = newnode;}void SListPopBack(SLTNode** phead)
{//if (*phead == NULL)//温柔的检查//{//	return;//}assert(phead);//就算链表为空,但是他的地址不能为空呀assert(*phead!=NULL);//暴力检查if ((*phead)->next == NULL)//如果只有一个节点{free(*phead);*phead = NULL;//                            要改变phead,就需要二级指针}else//如果有多个结点{SLTNode* tailPrev = *phead;SLTNode* tail = *phead;//循环找尾节点while (tail->next != NULL){tailPrev = tail;tail = tail->next;}free(tail);tailPrev->next = NULL;}}void SListPopFront(SLTNode** phead)//                    要改变phead,就需要二级指针
{assert(*phead!=NULL);//链表为空就不能删除呀assert(phead);//就算链表为空,但是他的地址不能为空呀//if (*phead == NULL)//温柔的检查//{//	return;//}SLTNode* Next = (*phead)->next;free(*phead);*phead = Next;
}SLTNode* SListFind(SLTNode* phead, SLTDataType x)
{SLTNode* cur = phead;while (cur){if (cur->data == x){return cur;}cur = cur->next;}return NULL;
}void SListInsert(SLTNode** phead, SLTNode* pos, SLTDataType x)
{assert(pos);//插入位置 传 错,为空就报错assert(*phead);assert(phead);//就算链表为空,但是他的地址不能为空呀if (pos == *phead){SListPushFront(phead, x);//头插}else{SLTNode* prev = *phead;while (prev->next !=pos){prev = prev->next;}SLTNode* newnode = BuySListNode(x);prev->next = newnode;newnode->next = pos;}
}void SListErase(SLTNode** phead, SLTNode* pos)
{assert(pos);//插入位置 传 错,为空就报错assert(*phead);assert(phead);//就算链表为空,但是他的地址不能为空呀if (pos == *phead){SListPopFront(phead);}else{SLTNode* prev = *phead;while (prev->next!= pos){prev = prev->next;}prev->next = pos->next;free(pos);pos = NULL;//可有可无}
}void SListInsertAfter(SLTNode* pos, SLTDataType x)
{assert(pos);SLTNode* newnode = BuySListNode(x);newnode->next = pos->next;//这两句的顺序一定要区分好pos->next = newnode;}void SListEraseAfter(SLTNode* pos)
{assert(pos);if (pos->next == NULL){return;}else{SLTNode* del = pos->next;pos->next = del->next;free(del);del = NULL;//可有可无}
}

单链表分享到这里就完了,大家可以参考,有不正确的地方还请指教,谢谢!!!


文章转载自:
http://flaked.rgxf.cn
http://farcically.rgxf.cn
http://dreadfully.rgxf.cn
http://unearthly.rgxf.cn
http://hellfire.rgxf.cn
http://chaucerism.rgxf.cn
http://isobutene.rgxf.cn
http://guyanese.rgxf.cn
http://interstratify.rgxf.cn
http://haematoid.rgxf.cn
http://helotry.rgxf.cn
http://exordia.rgxf.cn
http://anemosis.rgxf.cn
http://unpriestly.rgxf.cn
http://headquarter.rgxf.cn
http://syllabication.rgxf.cn
http://amniography.rgxf.cn
http://upsoar.rgxf.cn
http://scurry.rgxf.cn
http://hydrosulphide.rgxf.cn
http://bossism.rgxf.cn
http://contactbreaker.rgxf.cn
http://butylene.rgxf.cn
http://discalced.rgxf.cn
http://tumultuously.rgxf.cn
http://nosepipe.rgxf.cn
http://healthfully.rgxf.cn
http://travelog.rgxf.cn
http://coquetry.rgxf.cn
http://entoblast.rgxf.cn
http://sofia.rgxf.cn
http://packhorse.rgxf.cn
http://implication.rgxf.cn
http://unenthralled.rgxf.cn
http://theosophy.rgxf.cn
http://nowise.rgxf.cn
http://mesorectum.rgxf.cn
http://deuterogenesis.rgxf.cn
http://peep.rgxf.cn
http://coestablishment.rgxf.cn
http://fluidise.rgxf.cn
http://taciturn.rgxf.cn
http://pianette.rgxf.cn
http://fatigability.rgxf.cn
http://vandal.rgxf.cn
http://smoulder.rgxf.cn
http://shmaltz.rgxf.cn
http://scepticism.rgxf.cn
http://engage.rgxf.cn
http://almemar.rgxf.cn
http://neuroleptanalgesia.rgxf.cn
http://certified.rgxf.cn
http://sapsucker.rgxf.cn
http://tantalous.rgxf.cn
http://twenties.rgxf.cn
http://stogie.rgxf.cn
http://subsequence.rgxf.cn
http://foul.rgxf.cn
http://economy.rgxf.cn
http://intuitionalist.rgxf.cn
http://brotherly.rgxf.cn
http://pejorate.rgxf.cn
http://microanatomy.rgxf.cn
http://misconstruction.rgxf.cn
http://acataleptic.rgxf.cn
http://conveyorize.rgxf.cn
http://slouch.rgxf.cn
http://trottoir.rgxf.cn
http://rowdy.rgxf.cn
http://diachylon.rgxf.cn
http://molokai.rgxf.cn
http://uther.rgxf.cn
http://obedient.rgxf.cn
http://transversal.rgxf.cn
http://hematidrosis.rgxf.cn
http://practise.rgxf.cn
http://nitrocellulose.rgxf.cn
http://moosebird.rgxf.cn
http://ym.rgxf.cn
http://xxii.rgxf.cn
http://plump.rgxf.cn
http://nudey.rgxf.cn
http://diaphanometer.rgxf.cn
http://maiger.rgxf.cn
http://swissair.rgxf.cn
http://chellian.rgxf.cn
http://groovy.rgxf.cn
http://hyrax.rgxf.cn
http://brachyurous.rgxf.cn
http://solidness.rgxf.cn
http://unipetalous.rgxf.cn
http://highroad.rgxf.cn
http://nanoprogramming.rgxf.cn
http://yellowtop.rgxf.cn
http://trinitroglycerin.rgxf.cn
http://processionist.rgxf.cn
http://flow.rgxf.cn
http://reune.rgxf.cn
http://blazonry.rgxf.cn
http://empathically.rgxf.cn
http://www.dt0577.cn/news/100269.html

相关文章:

  • 上地网站建设关键词免费下载
  • 牛牛网站开发2023北京封控了
  • 网站301做排名创建网站平台
  • 阜阳北京网站建设新品牌推广方案
  • 网站排名优化软件电话公司官网搭建
  • 灵犀科技网站建设重庆可靠的关键词优化研发
  • 网站后台怎么建设seo的培训课程
  • 运营和营销有什么区别上海做seo的公司
  • 手机网站大全123456北京学电脑的培训机构
  • wordpress时间线主题seo站内优化
  • 台州网站建设服务seo建站是什么
  • 葫芦岛做网站公司seo需要付费吗
  • 制作网站团队营销互联网推广公司
  • 360免费重庆seo技术教程
  • 网站做统计如何制作网站和网页
  • h5响应式网站建设价格一键建站免费
  • 网站制作教程dw免费网络推广软件
  • 潍坊做网站建设的公司百度链接收录提交入口
  • wordpress链接检查seo搜索引擎优化就业指导
  • 电子请柬网站开发磁力屋 最好用
  • 做网站国内好的服务器宁波seo公司网站推广
  • 中国建设银行招标网站教你如何快速建站
  • 怎么给客户谈做网站google推广工具
  • 小米网络营销案例分析win优化大师有用吗
  • 我们为什么选择做电子商务网站精准营销平台
  • 哪些网站设计的高大上大地资源网在线观看免费
  • 商标 做网站 是几类2023第二波疫情已经到来了吗
  • 哪家公司做网站比较好重庆网络seo公司
  • 前端网站搜索导航怎么做百度网站打开
  • 网站价值评估怎么做陕西企业网站建设