Python单链表简单实现代码


Posted in Python onApril 27, 2016

本文实例讲述了Python单链表简单实现代码。分享给大家供大家参考,具体如下:

用Python模拟一下单链表,比较简单,初学者可以参考参考

#coding:utf-8
class Node(object):
  def __init__(self, data):
    self.data = data
    self.next = None
class NodeList(object):
  def __init__(self, node):
    self.head = node
    self.head.next = None
    self.end = self.head
  def add_node(self, node):
    self.end.next = node
    self.end = self.end.next
  def length(self):
    node = self.head
    count = 1
    while node.next is not None:
      count += 1
      node = node.next
    return count
  # delete node and return it's value
  def delete_node(self, index):
    if index+1 > self.length():
      raise IndexError('index out of bounds')
    i = 0
    node = self.head
    while True:
      if i==index-1:
        break
      node = node.next
      i += 1
    tmp_node = node.next
    node.next = node.next.next
    return tmp_node.data
  def show(self):
    node = self.head
    node_str = ''
    while node is not None:
      if node.next is not None:
        node_str += str(node.data) + '->'
      else:
        node_str += str(node.data)
      node = node.next
    print node_str
  # Modify the original position value and return the old value
  def change(self, index, data):
    if index+1 > self.length():
      raise IndexError('index out of bounds')
    i = 0
    node = self.head
    while True:
      if i == index:
        break
      node = node.next
      i += 1
    tmp_data = node.data
    node.data = data
    return tmp_data
  # To find the location of index value
  def find(self, index):
    if index+1 > self.length():
      raise IndexError('index out of bounds')
    i = 0
    node = self.head
    while True:
      if i == index:
        break
      node = node.next
      i += 1
    return node.data
#test case
n1 = Node(0)
n2 = Node(1)
n3 = Node(2)
n4 = Node(3)
n5 = Node(4)
node_list = NodeList(n1)
node_list.add_node(n2)
node_list.add_node(n3)
node_list.add_node(n4)
node_list.add_node(n5)
#node = node_list.delete_node(3)
#print node
#d = node_list.change(0,88)
data = node_list.find(5)
print data
node_list.show()

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
把项目从Python2.x移植到Python3.x的经验总结
Apr 20 Python
用Python编写一个每天都在系统下新建一个文件夹的脚本
May 04 Python
python Matplotlib画图之调整字体大小的示例
Nov 20 Python
Python用 KNN 进行验证码识别的实现方法
Feb 06 Python
通过shell+python实现企业微信预警
Mar 07 Python
Python弹出输入框并获取输入值的实例
Jun 18 Python
django中账号密码验证登陆功能的实现方法
Jul 15 Python
python 内置函数汇总详解
Sep 16 Python
python使用Geany编辑器配置方法
Feb 21 Python
Python GUI编程学习笔记之tkinter控件的介绍及基本使用方法详解
Mar 30 Python
python 中关于pycharm选择运行环境的问题
Oct 31 Python
安装pytorch时报sslerror错误的解决方案
May 17 Python
python版本的读写锁操作方法
Apr 25 #Python
Python简单实现enum功能的方法
Apr 25 #Python
Python爬虫辅助利器PyQuery模块的安装使用攻略
Apr 24 #Python
Linux下通过python访问MySQL、Oracle、SQL Server数据库的方法
Apr 23 #Python
Python 中的 else详解
Apr 23 #Python
Python 探针的实现原理
Apr 23 #Python
一键搞定python连接mysql驱动有关问题(windows版本)
Apr 23 #Python
You might like
给海燕B411配件机起死回生配上件
2021/03/02 无线电
第四节 构造函数和析构函数 [4]
2006/10/09 PHP
Ubuntu12下编译安装PHP5.3开发环境
2015/03/27 PHP
浅谈PHP eval()函数定义和用法
2016/06/21 PHP
php禁用cookie后session设置方法分析
2016/10/19 PHP
Laravel使用PHPQRCODE实现生成带有LOGO的二维码图片功能示例
2017/07/07 PHP
thinkphp分页集成实例
2017/07/24 PHP
javascript 文章截取部分无损html显示实现代码
2010/05/04 Javascript
js获取系统的根路径实现介绍
2013/09/08 Javascript
AngularJS ng-bind-template 指令详解
2016/07/30 Javascript
JS实现的驼峰式和连字符式转换功能分析
2016/12/21 Javascript
JS实现线性表的链式表示方法示例【经典数据结构】
2017/04/11 Javascript
浅谈vue,angular,react数据双向绑定原理分析
2017/11/28 Javascript
详解如何实现一个简单的Node.js脚手架
2017/12/04 Javascript
react在安卓中输入框被手机键盘遮挡问题的解决方法
2018/09/03 Javascript
微信小程序前端promise封装代码实例
2019/08/24 Javascript
微信小程序 可搜索的地址选择实现详解
2019/08/28 Javascript
使用python开发vim插件及心得分享
2014/11/04 Python
详解Python中的正则表达式
2018/07/08 Python
Python运维自动化之nginx配置文件对比操作示例
2018/08/29 Python
python 随机生成10位数密码的实现代码
2019/06/27 Python
python多继承(钻石继承)问题和解决方法简单示例
2019/10/21 Python
基于python实现操作git过程代码解析
2020/07/27 Python
Python list和str互转的实现示例
2020/11/16 Python
匡威爱尔兰官网:Converse爱尔兰
2019/06/09 全球购物
党员政治学习材料
2014/05/14 职场文书
另类冲刺标语
2014/06/24 职场文书
购房委托书范本
2014/09/18 职场文书
农业局党的群众路线教育实践活动整改方案
2014/09/20 职场文书
未婚证明书模板
2014/10/08 职场文书
2015年依法行政工作总结
2015/04/29 职场文书
父亲去世追悼词
2015/06/23 职场文书
如何利用JavaScript实现二叉搜索树
2021/04/02 Javascript
再谈python_tkinter弹出对话框创建
2022/03/20 Python
Win11无法访问设备和打印机 如何解决页面空白
2022/04/09 数码科技
Mysql调整优化之四种分区方式以及组合分区
2022/04/13 MySQL