Python实现数据结构
文章目录
- 一、Python实现数据结构
- 1.1 python实现单向链表
- 1.2 python实现单向循环链表
- 1.3 python实现双向链表
一、Python实现数据结构
1.1 python实现单向链表
singleLinkedList.py
class SingleNode:"""the node of single link list"""def __init__(self, item):self.item = itemself.next = Nonedef __str__(self):return str(self.item)class SingleLinkList:"""sing link list"""def __init__(self):self._head = Nonedef is_empty(self):"""判断链表是否为空"""return self._head is Nonedef length(self):"""获取链表长度"""cur = self._headcount = 0while cur is not None:count += 1cur = cur.nextreturn countdef travel(self):"""遍历链表"""cur = self._headwhile cur is not None:print(cur.item)cur = cur.nextprint()def add(self, item):"""链表头部添加元素"""node = SingleNode(item)node.next = self._headself._head = nodedef append(self, item):"""链表尾部添加元素"""node = SingleNode(item)if self.is_empty():self._head = nodeelse:cur = self._headwhile cur.next is not None:cur = cur.next"""此时cur指向最后一个节点,next=None"""cur.next = nodedef insert(self, pos, item):"""指定位置添加元素"""# 若pos小于0,则执行头部插入if pos <= 0:self.add(item)# 若pos大鱼链表长度,则执行尾部插入elif pos > self.length() - 1:self.append(item)else:node = SingleNode(item)cur = self._headcur_pos = 0while cur.next is not None:"""获取插入位置的上一个节点"""if pos - 1 == cur_pos:node.next = cur.nextcur.next = nodebreakcur = cur.nextcur_pos += 1def remove(self, item):"""删除节点"""if self.is_empty():returncur = self._headif cur.item == item:self._head = cur.nextelse:while cur.next is not None:if cur.next.item == item:cur.next = cur.next.nextbreakcur = cur.nextdef search(self, item):"""查找节点位置"""cur = self._headcount = 0while cur is not None:if cur.item == item:return countcur = cur.nextcount += 1return -1# if __name__ == "__main__":
# ll = SingleLinkList()
# ll.add(1)
# ll.add(2)
# ll.append(3)
# ll.insert(2,4)
# print("length: ", ll.length())
# ll.travel()
# print("search(3): ", ll.search(3))
# print("search(5): ", ll.search(5))
# ll.remove(1)
# print("length: ", ll.length())
# ll.travel()
1.2 python实现单向循环链表
sinCycLinkedList.py
class Node:def __init__(self, item):self.item = itemself.next = Nonedef __str__(self):return str(self.item)class SinCycLinkedList:"""单向循环链表"""def __init__(self):self._head = Nonedef is_empty(self):"""判断链表受否为空"""return self._head is Nonedef length(self):"""返回链表长度"""if self.is_empty():return 0cur = self._headcount = 1while cur.next != self._head:count += 1cur = cur.nextreturn countdef travel(self):"""遍历链表"""if self.is_empty():returncur = self._headprint(cur.item)while cur.next != self._head:cur = cur.nextprint(cur.item)print()def add(self, item):"""链表头部添加节点"""node = Node(item)if self.is_empty():self._head = nodenode.next = nodeelse:cur = self._headnode.next = self._headwhile cur.next != self._head:cur = cur.nextcur.next = nodeself._head = nodedef append(self, item):"""链表尾部添加节点"""node = Node(item)if self.is_empty():self._head = nodenode.next = self._headelse:cur = self._headwhile cur.next != self._head:cur = cur.nextcur.next = nodenode.next = self._headdef insert(self, pos, item):"""链表指定位置插入节点"""if pos <= 0:self.add(item)elif pos > self.length() - 1:self.append(item)else:cur = self._headcur_pos = 0node = Node(item)while cur.next != self._head:if cur_pos == pos - 1:node.next = cur.nextcur.next = nodebreakcur = cur.nextcur_pos += 1def remove(self, item):"""删除链表指定节点"""if self.is_empty():returnpre = self._headif pre.item == item:cur = prewhile cur.next != pre:cur = cur.nextcur.next = pre.nextself._head = pre.nextelse:cur = prewhile cur.next != pre:if cur.next.item == item:cur.next = cur.next.next# breakcur = cur.nextdef search(self, item):"""查找节点,返回下标"""cur = self._headcount = 0while cur.next != self._head:if cur.item == item:return countcount += 1cur = cur.nextreturn -1# if __name__ == "__main__":
# ll = SinCycLinkedList()
# ll.add(1)
# ll.add(2)
# ll.travel()
# ll.append(3)
# ll.insert(2, 4)
# ll.insert(4, 5)
# ll.insert(0, 6)
# print("length ", ll.length())
# ll.travel()
# print("search(3)", ll.search(3))
# print("search(7)", ll.search(7))
# print("search(6)", ll.search(6))
# print("remove(1)")
# ll.remove(1)
# print("length: ", ll.length())
# print("remove(6)")
# ll.remove(6)
# ll.travel()
1.3 python实现双向链表
doubleLinkedList.py
class Node:def __init__(self, item):self.item = itemself.previous = Noneself.next = Nonedef __str__(self):return str(self.item)class DLinkedList:"""双向链表"""def __init__(self):self._head = Nonedef is_empty(self):"""判断链表是否为空"""return self._head is Nonedef length(self):"""返回链表长度"""if self.is_empty():return 0count = 1cur = self._headwhile cur.next is not None:count += 1cur = cur.nextreturn countdef travel(self):"""遍历链表"""if self.is_empty():returncur = self._headprint(cur.item)while cur.next is not None:cur = cur.nextprint(cur.item)print()def add(self, item):"""链表头部添加节点"""node = Node(item)if self.is_empty():self._head = nodeelse:node.next = self._headself._head.previous = nodeself._head = nodedef append(self, item):"""链表尾部添加节点"""node = Node(item)if self.is_empty():self._head = nodeelse:cur = self._headwhile cur.next is not None:cur = cur.nextcur.next = nodenode.previous = curdef insert(self, pos, item):"""链表指定位置插入节点"""if pos <= 0:self.add(item)elif pos > self.length() - 1:self.append(item)else:cur = self._headnode = Node(item)cur_pos = 0while cur is not None:if cur_pos == pos - 1:node.next = cur.nextnode.previous = curcur.next = nodecur.next.previous = nodebreakcur = cur.nextcur_pos += 1def remove(self, item):"""链表删除指定元素"""if self.is_empty():returncur = self._headif cur.item == item:self._head = cur.nextself._head.previous = Noneelse:while cur.next is not None:if cur.item == item:cur.previous.next = cur.nextcur.next.previous = cur.previousreturncur = cur.nextif cur.item == item:cur.previous.next = Nonedef search(self, item):"""查找链表指定元素,返回元素下标"""cur_pos = 0cur = self._headwhile cur is not None:if cur.item == item:return cur_poscur = cur.nextcur_pos += 1return -1if __name__ == "__main__":ll = DLinkedList()ll.add(1)ll.add(2)ll.append(3)ll.insert(2, 4)ll.insert(4, 5)ll.insert(0, 6)print("length: ", ll.length()) ll.travel()print("search(3) ", ll.search(3))print("search(4) ", ll.search(4))print("search(10) ", ll.search(10))ll.remove(1)print("length: ", ll.length())ll.travel()print("删除首节点 remove(6): ")ll.remove(6)ll.travel()print("删除尾节点 remove(5): ")ll.remove(5)ll.travel()
相关文章:
Python实现数据结构
文章目录 一、Python实现数据结构1.1 python实现单向链表1.2 python实现单向循环链表1.3 python实现双向链表 一、Python实现数据结构 1.1 python实现单向链表 singleLinkedList.py class SingleNode:"""the node of single link list"""def …...
esp32CAM环境安装教程---串口驱动安装
前言 (1)本人安装好arduino 的ESP32环境之后, 发现一直下载不进去程序。一直说Cannot configure port, something went wrong. Original message: PermissionError。 (2)查阅了很多资料,用了各种办法&#…...
Java中List和Array转换
文章目录 List -> Array1. 调用toArray()方法直接返回一个Object[]数组:2. 给toArray(T[])传入一个类型相同的Array,List内部自动把元素复制到传入的Array中:3. 通过List接口定义的T[] toArray(IntFunction<T[]> generator)方法&…...
如何能确定数据库中root用户的密码是什么
如果您无法确定数据库中 root 用户的密码,有几种方法可以尝试找回或重置密码: 1. 使用已知密码:如果您有之前设置的 root 用户密码,可以使用该密码进行登录。 2. 查找密码文件:在某些情况下,MariaDB 可能…...
由浅入深Netty协议设计与解析
目录 1 为什么需要协议?2 redis 协议举例3 http 协议举例4 自定义协议要素4.1 编解码器4.2 什么时候可以加 Sharable 1 为什么需要协议? TCP/IP 中消息传输基于流的方式,没有边界。 协议的目的就是划定消息的边界,制定通信双方要…...
iptables防火墙(1)
iptables防火墙 一、iptables概述二、netfilter/iptables 关系三、四表五链1.四表2.五链 四、规则链之间的匹配顺序五、规则链内的匹配顺序六、iptables安装与配置七、常用的控制类型八、常用的管理选项九、规则命令1.添加新规则2.查看规则列表3.设置默认策略4.删除规则5.清空规…...
第九章 Productions最佳实践 - Productions开发的最佳实践
文章目录 第九章 Productions最佳实践 - Productions开发的最佳实践Productions开发的最佳实践项目目标项目交付文档 第九章 Productions最佳实践 - Productions开发的最佳实践 Productions开发的最佳实践 本章是一个总体概述,旨在帮助团队成员为从事生产项目做好…...
RocketMQ 怎么实现的消息负载均衡以及怎么能够保证消息被顺序消费
一、RocketMQ 怎么实现的消息负载均衡 RocketMQ是一种开源的分布式消息中间件,它使用了一种称为消息负载均衡的机制来实现消息的分发和消费的负载均衡。RocketMQ的消息负载均衡主要是通过以下两个方面实现的: 消息队列分组(Message Queue G…...
【随笔记】全志 T507 PF4 引脚无法被正常设置为中断模式的问题分析
相关信息 硬件平台:全志T507 系统版本:Android 10 / Linux 4.9.170 问题描述:PF4 无法通过标准接口设置为中断模式,而 PF1、PF2、PF3、PF5 正常可用。 分析过程 一开始以为是引脚被其它驱动占用引起,或者该引脚不具…...
人手一个 Midjourney,StableStudio 重磅开源!
人手一个 Midjourney,StableStudio 重磅开源! Stability AI 公司在上个月 19 号推出了 Alpha 版本 StableLM 大语言模型,包含了 30 亿和 70 亿参数,并且支持商用。如今他们再次推出了 AI 图像生成平台 StableStudio,这…...
iptables防火墙(2)
iptables防火墙(2) 一、SNATSNAT应用环境SNAT原理SNAT转换前条件扩展 二、DNATDNAT应用环境DNAT原理DNAT转换前提条件扩展 三、防火墙规则的备份和还原导出(备份)所有表的规则导入(还原)规则 一、SNAT SNA…...
Windows和Kali上使用proxychains代理流量
Windows和Kali上使用proxychains代理流量 PS. 本文演示都是在kali进行的,如有出入还请联系我哦1. Linux(Debian)1.1. 检查一下是否有proxychains1.2 修改config文件 2. Linux(Debian)安装proxychians43. Windows3.1 下载3.2 配置 4. Windows下的配置5. 测试 PS. 写这…...
KEYSIGHT MSOS204A 2GHZ 4通道DSOS204A高清晰度示波器
KEYSIGHT是德DSOS204A/MSOS204A高清晰度示波器 附加功能: 2 GHz 带宽(可升级) 4 个模拟通道和 16 个数字通道 最大存储深度:800 Mpts(2 通道),400 Mpts(4 通道) 最大…...
最新Java适配商城系统
城前端功能展示 商城移动端 后端基于SpringBoot 研发,前端使用 Vue、uniapp开发 前后端分离,支持分布式部署,支持Docker,各个API独立,并且有独立的消费者 api不需要单独部署,只需启动一个jar包就可以正…...
【KVM虚拟化】· virsh管理命令
目录 🍁libvirt架构概述 🍁使用virsh管理虚拟机 🍂常用命令总结 🍁kvm基本功能管理 🍂帮助命令 🍂KVM的配置文件存放目录 🍂查看虚拟机状态 🍂虚拟机关机与开机 🍂强制虚…...
JS Es6中判断b数组对象是否有跟a数组对象相同的数值(例如:id),有的话就过滤掉
如下[数组]对象a和b let a[{id:1,value:this},{id:2,value:is}] let b[{id:1,value:hello},{id:3,value:world}]filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。 some() 方法用于检测数组中的元素是否满足指定条件&#x…...
python获取某电商平台口红数据并制作词云
目录标题 前言开发环境:模块使用数据来源分析代码展示获取数据制作词云 尾语 💝 前言 嗨喽~大家好呀,这里是魔王呐 ❤ ~! 开发环境: Python 3.8 Pycharm 模块使用 requests jieba 结巴分词 wordcloud 词云 第三方模块安装: win R 输…...
阿里成立AIDC,用“增长”解题国际化
随着阿里巴巴集团2023财年年报的披露,AIDC也随即浮出了水面。 AIDC是阿里国际数字商业集团的英文简称,AIDC即Alibaba International Digital Commerce。阿里是在5月18日公布的截至2023年3月31日的2023财年Q4及全年财报,财报数据之外ÿ…...
全面理解:在计算机科学中同步、异步、并行、并发,他们之间到底有什么区别,如果正确更好的区分它们?
同步,异步,并行,并发的基础概念 在计算机中同步的基础概念 在计算机科学中,同步(Synchronization)是指在多个过程或线程中,它们的执行在时间上是有序的。换句话说,要执行一个特定的…...
9、Ray核心框架介绍
9、Ray核心框架介绍 导航 1.简介和背景 2.Ray的基本概念和核心组件 3.分布式任务调度和依赖管理 4.对象存储和数据共享 5.Actor模型和并发编程 6.Ray的高级功能和扩展性 7.使用Ray构建分布式应用程序的案例研究 8.Ray社区和资源 9.核心框架介绍 10.扩展1...
构建智能音乐档案:SoundCloud Downloader 的技术架构与实现哲学
构建智能音乐档案:SoundCloud Downloader 的技术架构与实现哲学 【免费下载链接】scdl Soundcloud Music Downloader 项目地址: https://gitcode.com/gh_mirrors/sc/scdl 在流媒体音乐主导的时代,音乐爱好者面临着一种矛盾:我们享受着…...
收藏|2026年大模型算法岗崛起!程序员小白入门高薪赛道全攻略
前些年,算法岗位一直稳居技术圈高薪行列,无数程序员争相入局,也成为计算机专业毕业生求职首选方向。 伴随大模型技术飞速迭代落地,行业就业格局迎来重大变革。如今含金量最高、人才缺口最大、长期发展潜力顶尖的岗位,已…...
Windows Cleaner如何5步解决C盘爆红问题?完全指南助你释放宝贵空间
Windows Cleaner如何5步解决C盘爆红问题?完全指南助你释放宝贵空间 【免费下载链接】WindowsCleaner Windows Cleaner——专治C盘爆红及各种不服! 项目地址: https://gitcode.com/gh_mirrors/wi/WindowsCleaner 你是否曾经面对C盘爆红的警告束手无…...
Windows 11终极优化指南:一键清理系统,释放51%性能潜力
Windows 11终极优化指南:一键清理系统,释放51%性能潜力 【免费下载链接】Win11Debloat A simple, lightweight PowerShell script that allows you to remove pre-installed apps, disable telemetry, as well as perform various other changes to decl…...
从自然语言到可视化洞察:ChartGPT如何用AI重构数据图表生成范式
从自然语言到可视化洞察:ChartGPT如何用AI重构数据图表生成范式 【免费下载链接】chart-gpt AI tool to build charts based on text input 项目地址: https://gitcode.com/gh_mirrors/ch/chart-gpt 在数据驱动的决策时代,业务人员与技术团队之间…...
保姆级教程:用Prometheus Operator在K8S里一键搞定监控全家桶(附Grafana仪表盘)
云原生监控革命:用Prometheus Operator构建K8S智能监控体系 当Kubernetes集群规模突破50个节点时,传统监控方案的维护成本会呈指数级增长。我曾亲眼见证一个电商团队在"黑五"大促期间,因为手动配置的Prometheus抓取规则失效&#x…...
深度解析Atmosphere系统:为Nintendo Switch带来的完整自定义固件解决方案
深度解析Atmosphere系统:为Nintendo Switch带来的完整自定义固件解决方案 【免费下载链接】Atmosphere-stable 大气层整合包系统稳定版 项目地址: https://gitcode.com/gh_mirrors/at/Atmosphere-stable 你是否曾为Switch游戏价格高昂而烦恼?是否…...
SuperMap iDesktop中BIM模型缓存生成全攻略:从性能调优到Web端流畅加载的避坑指南
SuperMap iDesktop中BIM模型缓存生成全攻略:从性能调优到Web端流畅加载的避坑指南 当你在深夜加班处理一个大型商业综合体的BIM模型时,iDesktop突然闪退,进度条停留在87%——这种崩溃瞬间是否似曾相识?作为经历过数十个大型BIM项目…...
为内容创作平台集成 AI 功能时利用 Taotoken 实现模型灵活调度
🚀 告别海外账号与网络限制!稳定直连全球优质大模型,限时半价接入中。 👉 点击领取海量免费额度 为内容创作平台集成 AI 功能时利用 Taotoken 实现模型灵活调度 在内容创作或媒体类平台中集成 AI 辅助功能,如文章生成…...
3D CNN与ITK-SNAP融合:实现肺结节三维体积自动量化的工程实践
1. 项目概述:从一维测量到三维量化的跨越在肺部CT影像的临床判读中,肺结节的评估一直是核心且充满挑战的环节。作为一名长期关注医学影像分析技术落地的从业者,我深刻体会到传统方法的局限性。过去,医生们主要依赖一维的实性成分最…...
