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

朝阳区手机网站制作服务永久域名查询

朝阳区手机网站制作服务,永久域名查询,遨游网站建设有限公司,360免费wifi不稳定一、链表 链表是一种线性数据结构,由一系列按特定顺序排列的节点组成,这些节点通过指针相互连接。每个节点包含两部分:元素和指向下一个节点的指针。其中,最简单的形式是单向链表,每个节点含有一个信息域和一个指针域&…

一、链表

        链表是一种线性数据结构,由一系列按特定顺序排列的节点组成,这些节点通过指针相互连接。每个节点包含两部分:元素和指向下一个节点的指针。其中,最简单的形式是单向链表,每个节点含有一个信息域和一个指针域,该指针指向链表中的下一个节点,最后一个节点则指向一个空值。

1.1、Python中的链表

•        大部分都是用C语言实现链表,因为C有指针,可以很方便的控制内存,很轻易就可以实现链 表,在C/C++中,通常采用“指针+结构体”来实现链表。

•        Python是一门动态语言,可以直接把对象赋值给新的变量,我们采用“引用+类”来实现 链表。

•        结构:data为自定义的数据,next为下一个节点的地址。

•        链表的种类:单向链表、双向链表、单向循环链表、双向循环链表。 

1.2、基本元素

节点:每个节点有两个部分,左边称为值域,存放用户数据;右边部分称为指针域,用来存 放指向下一个元素的指针。

Head:head节点永远指向第一个节点。

Tail:tail节点永远指向最后一个节点。

None:链表中最后一个节点的指针域为None。

二、基本操作

2.1、节点的创建

class Node:def __init__(self, data):# 初始化节点,包含数据和指向下一个节点的指针self.data = dataself.next = None  

2.2、初始化单向链表

class LinkList:def __init__(self, node=None):# 初始化链表,头指针指向第一个节点(默认为None)self.__head = node

2.3、判断是否为空

    def is_empty(self):# 检查链表是否为空return self.__head == None

2.4、链表长度

    def length(self):# 计算链表的长度current = self.__headcount = 0while current != None:count += 1current = current.nextreturn count

2.5、遍历链表 

    def travel(self):# 遍历链表并打印每个节点的数据current = self.__headwhile current != None:print(current.data)current = current.next

2.4、插入节点

2.4.1、头节点

    def add(self, data):# 在链表头部添加新节点new_node = Node(data)new_node.next = self.__headself.__head = new_node

2.4.2、中间节点

    def insert(self, pos, data):# 在指定位置插入新节点if pos > self.length() - 1:self.append(data)  # 如果位置超出范围,添加到尾部elif pos <= 0:self.add(data)  # 如果位置小于等于0,添加到头部else:new_node = Node(data)pre = self.__headcount = 0while count < pos - 1:count += 1pre = pre.nextnew_node.next = pre.next  # 将新节点的next指向当前节点的nextpre.next = new_node  # 将前一个节点的next指向新节点

2.4.3、尾节点

    def append(self, data):# 在链表尾部添加新节点new_node = Node(data)if self.is_empty():self.__head = new_nodeelse:current = self.__headwhile current.next != None:current = current.nextcurrent.next = new_node

 

2.5、删除节点

    def remove(self, data):# 移除链表中指定数据的节点current = self.__headpre = Nonewhile current != None:if current.data == data:if current == self.__head:self.__head = current.next  # 如果是头节点,更新头指针else:pre.next = current.next  # 将前一个节点的next指向当前节点的nextbreakelse:pre = currentcurrent = current.next

2.6、查找节点

    def serch(self, data):# 查找链表中是否存在指定数据current = self.__headwhile current != None:if current.data == data:return True  # 找到数据,返回Trueelse:current = current.nextreturn False  # 遍历完成未找到数据,返回False

三、链表的特点

1、动态数据集合:链表允许动态的添加和删除元素,这使得它们在处理未知数量或频繁变 化的数据集时非常有用。

2、元素顺序:链表可以按照插入顺序来保持元素的顺序,这对于需要维护元素插入顺序的 应用程序非常有用。

3、 内存效率:与数组相比,链表在内存使用上更为高效,因为它们不需要连续的内存空间。 链表通过节点之间的指针来连接元素,这样可以更有效地利用内存空间。

4、避免数组扩容:数组在初始化时需要指定大小,如果超出大小,则需要扩容,这是一个 昂贵的操作。链表则可以避免这个问题,因为它们可以动态地增长。

四、单向链表的缺点

1、只能从头遍历到尾或者从尾遍历到头(一般从头到尾)。

2、链表相连的过程是单向的。实现的原理是上一个链表中有一个指向下一个的引用。

3、 我们可以轻松的到达下一个节点, 但是回到前一个节点是很难的。但是, 在实际开发中, 经 常会遇到需要回到上一个节点的情况。

举个例子:

        假设一个文本编辑用链表来存储文本。每一行用一个String对象存储在链表的 一个节点中。当编辑器用户向下移动光标时, 链表直接操作到下一个节点即可。但是当用 于将光标向上移动呢? 这个时候为了回到上一个节点, 我们可能需要从头开始, 依次走到 想要的节点上。

五、完整代码 

class Node:def __init__(self, data):# 初始化节点,包含数据和指向下一个节点的指针self.data = dataself.next = Noneclass LinkList:def __init__(self, node=None):# 初始化链表,头指针指向第一个节点(默认为None)self.__head = nodedef is_empty(self):# 检查链表是否为空return self.__head == Nonedef length(self):# 计算链表的长度current = self.__headcount = 0while current != None:count += 1current = current.nextreturn countdef travel(self):# 遍历链表并打印每个节点的数据current = self.__headwhile current != None:print(current.data)current = current.nextdef add(self, data):# 在链表头部添加新节点new_node = Node(data)new_node.next = self.__headself.__head = new_nodedef append(self, data):# 在链表尾部添加新节点new_node = Node(data)if self.is_empty():self.__head = new_nodeelse:current = self.__headwhile current.next != None:current = current.nextcurrent.next = new_nodedef insert(self, pos, data):# 在指定位置插入新节点if pos > self.length() - 1:self.append(data)  # 如果位置超出范围,添加到尾部elif pos <= 0:self.add(data)  # 如果位置小于等于0,添加到头部else:new_node = Node(data)pre = self.__headcount = 0while count < pos - 1:count += 1pre = pre.nextnew_node.next = pre.next  # 将新节点的next指向当前节点的nextpre.next = new_node  # 将前一个节点的next指向新节点def remove(self, data):# 移除链表中指定数据的节点current = self.__headpre = Nonewhile current != None:if current.data == data:if current == self.__head:self.__head = current.next  # 如果是头节点,更新头指针else:pre.next = current.next  # 将前一个节点的next指向当前节点的nextbreakelse:pre = currentcurrent = current.nextdef serch(self, data):# 查找链表中是否存在指定数据current = self.__headwhile current != None:if current.data == data:return True  # 找到数据,返回Trueelse:current = current.nextreturn False  # 遍历完成未找到数据,返回Falseif __name__ == '__main__':linklist = LinkList()linklist.add(10)  # 添加节点10linklist.add(20)  # 添加节点20linklist.append(100)  # 在尾部添加节点100linklist.add(30)  # 添加节点30linklist.add(40)  # 添加节点40print(linklist.is_empty())  # 检查链表是否为空print(linklist.length())  # 输出链表长度print('*****************')linklist.remove(30)  # 移除节点30# linklist.travel()  # 遍历链表并打印节点数据print(linklist.serch(40))  # 查找节点40是否存在# linklist.travel()  # 遍历链表并打印节点数据

六、思维导图 


文章转载自:
http://ornithomancy.c7622.cn
http://duplicator.c7622.cn
http://bedevilment.c7622.cn
http://lytic.c7622.cn
http://filaceous.c7622.cn
http://linaceous.c7622.cn
http://confluence.c7622.cn
http://cloggy.c7622.cn
http://raja.c7622.cn
http://similarity.c7622.cn
http://necrophore.c7622.cn
http://yestermorn.c7622.cn
http://filum.c7622.cn
http://wifedom.c7622.cn
http://tracheary.c7622.cn
http://hematozoon.c7622.cn
http://agoraphobe.c7622.cn
http://oiliness.c7622.cn
http://orange.c7622.cn
http://canner.c7622.cn
http://unpropitious.c7622.cn
http://scene.c7622.cn
http://fontainebleau.c7622.cn
http://rendzina.c7622.cn
http://monolatrist.c7622.cn
http://operative.c7622.cn
http://horsetail.c7622.cn
http://balata.c7622.cn
http://standee.c7622.cn
http://ommatidium.c7622.cn
http://iamb.c7622.cn
http://cdi.c7622.cn
http://predominate.c7622.cn
http://tetramethyl.c7622.cn
http://looseness.c7622.cn
http://rebunk.c7622.cn
http://patronage.c7622.cn
http://deductive.c7622.cn
http://runelike.c7622.cn
http://unnecessaries.c7622.cn
http://hazardous.c7622.cn
http://onychia.c7622.cn
http://enterprising.c7622.cn
http://perfectability.c7622.cn
http://omphalitis.c7622.cn
http://thundershower.c7622.cn
http://protozoan.c7622.cn
http://grift.c7622.cn
http://shufty.c7622.cn
http://boatage.c7622.cn
http://foliolate.c7622.cn
http://lated.c7622.cn
http://preamplifier.c7622.cn
http://amative.c7622.cn
http://rotorcraft.c7622.cn
http://exploitative.c7622.cn
http://appellor.c7622.cn
http://methodic.c7622.cn
http://radiology.c7622.cn
http://campfire.c7622.cn
http://selfhood.c7622.cn
http://tampere.c7622.cn
http://marshy.c7622.cn
http://privatism.c7622.cn
http://ordinee.c7622.cn
http://sporty.c7622.cn
http://biocytin.c7622.cn
http://ruined.c7622.cn
http://intermittently.c7622.cn
http://sempre.c7622.cn
http://presentability.c7622.cn
http://dystrophia.c7622.cn
http://delphin.c7622.cn
http://myriad.c7622.cn
http://overactive.c7622.cn
http://telephotometer.c7622.cn
http://dermatotherapy.c7622.cn
http://bonhomie.c7622.cn
http://graphotype.c7622.cn
http://nitration.c7622.cn
http://lobster.c7622.cn
http://separation.c7622.cn
http://nihilist.c7622.cn
http://melanesia.c7622.cn
http://resubject.c7622.cn
http://admire.c7622.cn
http://swathe.c7622.cn
http://ecology.c7622.cn
http://myopy.c7622.cn
http://rich.c7622.cn
http://liquory.c7622.cn
http://springhare.c7622.cn
http://rentier.c7622.cn
http://cockbrain.c7622.cn
http://unmutilated.c7622.cn
http://rushwork.c7622.cn
http://terbium.c7622.cn
http://microdontism.c7622.cn
http://independency.c7622.cn
http://naturalization.c7622.cn
http://www.zhongyajixie.com/news/98334.html

相关文章:

  • 上海网站建设中seo网站排名优化公司
  • 用vs代码做网站怎么免费制作网站
  • 20年的域名做网站怎么样网站推广什么意思
  • 网页美工设计核心素养广州网站优化公司
  • 网站建设所需要的材料百度自助建站官网
  • 网站标题结构自助友链平台
  • wordpress增加百度收录国内好的seo
  • 建筑行业新闻资讯西安百度快照优化
  • wordpress音乐播放器百度竞价推广账户优化
  • 品牌推广网站怎么做电商怎么做
  • 永康住房和城乡建设局网站nba体育新闻
  • 网页设计项目案例网站成都seo排名
  • 如何对网站做压力测试seo sem是什么意思
  • 前端和网站开发的区别seo优化必备技巧
  • 网站建设策划书百度文库恶意点击广告软件
  • 微信分销网站开发学电脑培训班
  • 北京网站策划联系电话企业网站怎么优化
  • 新手学做网站需要注意的几点网站推广策划报告
  • 建立香港网站空间网站步骤做一个app软件大概要多少钱
  • 朔州网站建设价格怎么引流怎么推广自己的产品
  • 2W网站建设的作用东莞网站推广行者seo08
  • 徐州优化网站建设关键词自动生成器
  • 做网站为什么很复杂seo视频教程我要自学网
  • 全国企业征信系统查询平台谷歌关键词排名优化
  • 男女做羞羞事漫画网站免费河南专业网站建设
  • 国外做电商网站有哪些方面世界足球世界排名
  • 深圳网站建站建设公司地址百度竞价推广是什么工作
  • 美妆网站模板深圳最新疫情最新消息
  • 四川同风源建设工程有限公司网站手机刷网站排名软件
  • 付费网站怎么破解佛山抖音seo