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...
LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器的上位机配置操作说明
LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器专为工业环境精心打造,完美适配AGV和无人叉车。同时,集成以太网与语音合成技术,为各类高级系统(如MES、调度系统、库位管理、立库等)提供高效便捷的语音交互体验。 L…...
谷歌浏览器插件
项目中有时候会用到插件 sync-cookie-extension1.0.0:开发环境同步测试 cookie 至 localhost,便于本地请求服务携带 cookie 参考地址:https://juejin.cn/post/7139354571712757767 里面有源码下载下来,加在到扩展即可使用FeHelp…...
智慧医疗能源事业线深度画像分析(上)
引言 医疗行业作为现代社会的关键基础设施,其能源消耗与环境影响正日益受到关注。随着全球"双碳"目标的推进和可持续发展理念的深入,智慧医疗能源事业线应运而生,致力于通过创新技术与管理方案,重构医疗领域的能源使用模式。这一事业线融合了能源管理、可持续发…...
【Oracle APEX开发小技巧12】
有如下需求: 有一个问题反馈页面,要实现在apex页面展示能直观看到反馈时间超过7天未处理的数据,方便管理员及时处理反馈。 我的方法:直接将逻辑写在SQL中,这样可以直接在页面展示 完整代码: SELECTSF.FE…...
376. Wiggle Subsequence
376. Wiggle Subsequence 代码 class Solution { public:int wiggleMaxLength(vector<int>& nums) {int n nums.size();int res 1;int prediff 0;int curdiff 0;for(int i 0;i < n-1;i){curdiff nums[i1] - nums[i];if( (prediff > 0 && curdif…...
linux 错误码总结
1,错误码的概念与作用 在Linux系统中,错误码是系统调用或库函数在执行失败时返回的特定数值,用于指示具体的错误类型。这些错误码通过全局变量errno来存储和传递,errno由操作系统维护,保存最近一次发生的错误信息。值得注意的是,errno的值在每次系统调用或函数调用失败时…...
如何将联系人从 iPhone 转移到 Android
从 iPhone 换到 Android 手机时,你可能需要保留重要的数据,例如通讯录。好在,将通讯录从 iPhone 转移到 Android 手机非常简单,你可以从本文中学习 6 种可靠的方法,确保随时保持连接,不错过任何信息。 第 1…...
06 Deep learning神经网络编程基础 激活函数 --吴恩达
深度学习激活函数详解 一、核心作用 引入非线性:使神经网络可学习复杂模式控制输出范围:如Sigmoid将输出限制在(0,1)梯度传递:影响反向传播的稳定性二、常见类型及数学表达 Sigmoid σ ( x ) = 1 1 +...
vue3+vite项目中使用.env文件环境变量方法
vue3vite项目中使用.env文件环境变量方法 .env文件作用命名规则常用的配置项示例使用方法注意事项在vite.config.js文件中读取环境变量方法 .env文件作用 .env 文件用于定义环境变量,这些变量可以在项目中通过 import.meta.env 进行访问。Vite 会自动加载这些环境变…...
Java线上CPU飙高问题排查全指南
一、引言 在Java应用的线上运行环境中,CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时,通常会导致应用响应缓慢,甚至服务不可用,严重影响用户体验和业务运行。因此,掌握一套科学有效的CPU飙高问题排查方法&…...
