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 中包/模块的 `import` 操作代码)
- pythonimage库(Python/Django后端使用PIL Image生成头像缩略图)
- python处理各种xml文件(Python使用sax模块解析XML文件示例)
- python语言支持多态(Python实现多态、协议和鸭子类型的代码详解)
- python支持面向对象的程序设计(Python面向对象程序设计之类的定义与继承简单示例)
- matlab嵌入python代码(python调用matlab的m自定义函数方法)
- pythondjango搭建web(PythonWeb项目Django部署在Ubuntu18.04腾讯云主机上)
- python中怎么实现登录程序(详解Python用户登录接口的方法)
- python scrapy爬虫教程视频(详解python3 + Scrapy爬虫学习之创建项目)
- python函数的参数有几种类型(在Python中居然可以定义两个同名参数的函数)
- python弹跳小球(python实现弹跳小球)
- 基于pythonopencv的图片识别(Python Opencv实现图像轮廓识别功能)
- python入门之字符串处理(Python中常用的8种字符串操作方法)
- 卷积神经网络python实现(Python通过TensorFlow卷积神经网络实现猫狗识别)
- 景区游玩,这些安全知识要牢记(这些安全知识要牢记)
- 各地出招烘 热 旅游(各地出招烘热旅游)
- 2021款起亚霸锐到店了 更换车标,竞争宝马X5有戏吗(2021款起亚霸锐到店了)
- 新款起亚霸锐实拍,比普拉多气派,配3.0T V6,引入国内或许能大卖(新款起亚霸锐实拍)
- ()
- 张勇 阿里新任掌门人(阿里新任掌门人)
热门推荐
- python快速数据分类(Python基于滑动平均思想实现缺失数据填充的方法)
- vue 怎么设置滚动条的长度(vue使用better-scroll实现横向滚动的方法实例)
- 小程序ui设计样式(AmazeUI 平滑滚动效果的示例代码)
- MongoDB中的索引的操作
- docker清理占用空间(docker清理大杀器/docker的overlay文件占用磁盘太大的解决)
- python format的用法(Python中format格式输出全解)
- html5 video标签
- mysql用什么类型数据(MySQL 数据类型选择原则)
- sql server2012下载安装教程(SQL Server 2012 安装与启动图文教程)
- sqlserverdatetime时区(解析SQL Server中datetimeset转换datetime类型问题)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9