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...
深入浅出Asp.Net Core MVC应用开发系列-AspNetCore中的日志记录
ASP.NET Core 是一个跨平台的开源框架,用于在 Windows、macOS 或 Linux 上生成基于云的新式 Web 应用。 ASP.NET Core 中的日志记录 .NET 通过 ILogger API 支持高性能结构化日志记录,以帮助监视应用程序行为和诊断问题。 可以通过配置不同的记录提供程…...
大话软工笔记—需求分析概述
需求分析,就是要对需求调研收集到的资料信息逐个地进行拆分、研究,从大量的不确定“需求”中确定出哪些需求最终要转换为确定的“功能需求”。 需求分析的作用非常重要,后续设计的依据主要来自于需求分析的成果,包括: 项目的目的…...
Prompt Tuning、P-Tuning、Prefix Tuning的区别
一、Prompt Tuning、P-Tuning、Prefix Tuning的区别 1. Prompt Tuning(提示调优) 核心思想:固定预训练模型参数,仅学习额外的连续提示向量(通常是嵌入层的一部分)。实现方式:在输入文本前添加可训练的连续向量(软提示),模型只更新这些提示参数。优势:参数量少(仅提…...
day52 ResNet18 CBAM
在深度学习的旅程中,我们不断探索如何提升模型的性能。今天,我将分享我在 ResNet18 模型中插入 CBAM(Convolutional Block Attention Module)模块,并采用分阶段微调策略的实践过程。通过这个过程,我不仅提升…...
【HTTP三个基础问题】
面试官您好!HTTP是超文本传输协议,是互联网上客户端和服务器之间传输超文本数据(比如文字、图片、音频、视频等)的核心协议,当前互联网应用最广泛的版本是HTTP1.1,它基于经典的C/S模型,也就是客…...
浅谈不同二分算法的查找情况
二分算法原理比较简单,但是实际的算法模板却有很多,这一切都源于二分查找问题中的复杂情况和二分算法的边界处理,以下是博主对一些二分算法查找的情况分析。 需要说明的是,以下二分算法都是基于有序序列为升序有序的情况…...
如何理解 IP 数据报中的 TTL?
目录 前言理解 前言 面试灵魂一问:说说对 IP 数据报中 TTL 的理解?我们都知道,IP 数据报由首部和数据两部分组成,首部又分为两部分:固定部分和可变部分,共占 20 字节,而即将讨论的 TTL 就位于首…...
Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决
Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决 问题背景 在一个基于 Spring Cloud Gateway WebFlux 构建的微服务项目中,新增了一个本地验证码接口 /code,使用函数式路由(RouterFunction)和 Hutool 的 Circle…...
云原生玩法三问:构建自定义开发环境
云原生玩法三问:构建自定义开发环境 引言 临时运维一个古董项目,无文档,无环境,无交接人,俗称三无。 运行设备的环境老,本地环境版本高,ssh不过去。正好最近对 腾讯出品的云原生 cnb 感兴趣&…...
React---day11
14.4 react-redux第三方库 提供connect、thunk之类的函数 以获取一个banner数据为例子 store: 我们在使用异步的时候理应是要使用中间件的,但是configureStore 已经自动集成了 redux-thunk,注意action里面要返回函数 import { configureS…...
