python怎么实现链表(Python实现数据结构线性链表单链表算法示例)
类别:脚本大全 浏览量:1388
时间:2021-10-06 01:36:08 python怎么实现链表
Python实现数据结构线性链表单链表算法示例本文实例讲述了python实现数据结构线性链表(单链表)算法。分享给大家供大家参考,具体如下:
初学python,拿数据结构中的线性链表存储结构练练手,理论比较简单,直接上代码。
|
#!/usr/bin/python # -*- coding:utf-8 -*- # author: hui # date: 2017-10-13 # 结点类, class node: def __init__( self , data): self .data = data # 数据域 self . next = none # 指针域 def get_data( self ): return self .data # 链表类 class list : def __init__( self , head): self .head = head # 默认初始化头结点 def is_empty( self ): # 空链表判断 return self .get_len() = = 0 def get_len( self ): # 返回链表长度 length = 0 temp = self .head while temp is not none: length + = 1 temp = temp. next return length def append( self , node): # 追加结点(链表尾部追加) temp = self .head while temp. next is not none: temp = temp. next temp. next = node def delete( self , index): # 删除结点 if index < 1 or index > self .get_len(): print "给定位置不合理" return if index = = 1 : self .head = self .head. next return temp = self .head cur_pos = 0 while temp is not none: cur_pos + = 1 if cur_pos = = index - 1 : temp. next = temp. next . next temp = temp. next def insert( self , pos, node): # 插入结点 if pos < 1 or pos > self .get_len(): print "插入结点位置不合理..." return temp = self .head cur_pos = 0 while temp is not node: cur_pos + = 1 if cur_pos = = pos - 1 : node. next = temp. next temp. next = node break temp = temp. next def reverse( self , head): # 反转链表 if head is none and head. next is none: return head pre = head cur = head. next while cur is not none: temp = cur. next cur. next = pre pre = cur cur = temp head. next = none return pre def print_list( self , head): # 打印链表 init_data = [] while head is not none: init_data.append(head.get_data()) head = head. next return init_data if __name__ = = '__main__' : head = node( "head" ) list = list (head) print '初始化头结点:\t' , list .print_list(head) for i in range ( 1 , 10 ): node = node(i) list .append(node) print '链表添加元素:\t' , list .print_list(head) print '链表是否空:\t' , list .is_empty() print '链表长度:\t' , list .get_len() list .delete( 9 ) print '删除第9个元素:\t' , list .print_list(head) node = node( "insert" ) list .insert( 3 , node) print '第3个位置插入‘insert' 字符串 :\t', list .print_list(head) head = list .reverse(head) print '链表反转:' , list .print_list(head) |
执行结果:
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/King0217/article/details/78228433
您可能感兴趣
- 如何用python在微信里自动回复(Python实现微信自动好友验证,自动回复,发送群聊链接方法)
- python中如何遍历键(Python中按值来获取指定的键)
- python数据结构全攻略(详解python数据结构和算法)
- python简易翻译器的运行(Python3.6实现带有简单界面的有道翻译小程序)
- python序列化图解(对Python3 序列解包详解)
- pythonpandas操作拆分excel(Python使用pandas和xlsxwriter读写xlsx文件的方法示例)
- python绘折线图数据(python2.7使用plotly绘制本地散点图和折线图)
- python装饰器使用说明(详解Python装饰器)
- python ip代理池(Python 实现域名解析为ip的方法)
- python处理excel基础教程第3讲(Python Excel处理库openpyxl使用详解)
- python中for循环计算1-100(python 用for循环实现1~n求和的实例)
- python中if条件语句如何使用(Python中如何使用if语句处理列表实例代码)
- centos7上安装python(centos6.5安装python3.7.1之后无法使用pip的解决方案)
- python基础教程常用函数整理(Python基础之函数的定义与使用示例)
- python使用django搭建简单网页(Python后台开发Django的教程详解启动)
- python sql注入怎么避免(Python实现SQL注入检测插件实例代码)
- 小米Watch S1评测 或许能成为小米冲击高端可穿戴设备的里程碑(小米WatchS1评测或许能成为小米冲击高端可穿戴设备的里程碑)
- 手机QQ与小米路由器在一起 明天揭晓,敬请期待(手机QQ与小米路由器在一起)
- 小米音乐与 QQ 音乐合作,便捷迁移会员(小米音乐与QQ音乐合作)
- 小米推出米兔儿童电话手表奥特曼版,799 元,支持微信 QQ(小米推出米兔儿童电话手表奥特曼版)
- 贾怀胤唱《白龙马》 炸场 了 没想到京剧还能这么玩(贾怀胤唱白龙马)
- 白龙马的改编学生版,快来看看(白龙马的改编学生版)
热门推荐
- pythonyield有什么用(彻底理解Python中的yield关键字)
- 宝塔面板怎么用ip访问站点(宝塔面板设置禁止通过IP直接访问网站防止恶意解析)
- mysql limit越大越慢(MySQL limit分页大偏移量慢的原因及优化方案)
- goweb框架哪个用的最多(教你搭建一个简单的Go Web服务器)
- phpstudy创建本地服务器(phpstudy linux小皮面板防火墙的开启与关闭)
- 个人能备案云服务器吗(租用云服务器后的备案问题你真的了解吗?)
- amazonawslinux服务器设置(如何在AWS EC2的Linux服务器上开放一个端口)
- 网站如何提高用户体验
- dedecms标签怎么用(dedecms三级栏目调用方法)
- 缓存系统机制(Smarty缓存机制实例详解三种缓存方式)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9