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 相关文章推荐
python编写暴力破解FTP密码小工具
Nov 19 Python
Python线程中对join方法的运用的教程
Apr 09 Python
简单讲解Python中的字符串与字符串的输入输出
Mar 13 Python
基于Python实现的微信好友数据分析
Feb 26 Python
Python定义一个跨越多行的字符串的多种方法小结
Jul 19 Python
Django分页查询并返回jsons数据(中文乱码解决方法)
Aug 02 Python
selenium+python自动化测试之页面元素定位
Jan 23 Python
浅谈PyTorch中in-place operation的含义
Jun 27 Python
python从ftp获取文件并下载到本地
Dec 05 Python
Python3爬虫RedisDump的安装步骤
Feb 20 Python
pandas数据分组groupby()和统计函数agg()的使用
Mar 04 Python
粗暴解决CUDA out of memory的问题
May 22 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
PHP数据库操作四:mongodb用法分析
2017/08/16 PHP
php微信公众号开发之简答题
2018/10/20 PHP
PHP 获取客户端 IP 地址的方法实例代码
2018/11/11 PHP
如何重写Laravel异常处理类详解
2020/12/20 PHP
JS实现图片横向滚动效果示例代码
2013/09/04 Javascript
实现51Map地图接口(示例代码)
2013/11/22 Javascript
浅谈setTimeout 与 setInterval
2015/06/23 Javascript
jQuery超赞的评分插件(8款)
2015/08/20 Javascript
jQuery实用密码强度检测
2017/03/02 Javascript
基于vue 开发中出现警告问题去除方法
2018/01/25 Javascript
微信小程序之多文件下载的简单封装示例
2018/01/29 Javascript
Angular5升级RxJS到5.5.3报错:EmptyError: no elements in sequence的解决方法
2018/04/09 Javascript
关于jquery中attr()和prop()方法的区别
2018/05/28 jQuery
支付宝小程序tabbar底部导航
2018/11/06 Javascript
vue项目打包后上传至GitHub并实现github-pages的预览
2019/05/06 Javascript
[02:30]DOTA2放量测试专访海涛:呼吁保护新手玩家
2013/08/26 DOTA
python 多线程应用介绍
2012/12/19 Python
举例介绍Python中的25个隐藏特性
2015/03/30 Python
Python整型运算之布尔型、标准整型、长整型操作示例
2017/07/21 Python
python opencv之SURF算法示例
2018/02/24 Python
python 获取图片分辨率的方法
2019/01/08 Python
Jupyter notebook运行Spark+Scala教程
2020/04/10 Python
python 错误处理 assert详解
2020/04/20 Python
Python操作Excel把数据分给sheet
2020/05/20 Python
HTML5实现动画效果的方式汇总
2016/02/29 HTML / CSS
印度最大的网上花店:Ferns N Petals(鲜花、礼品和蛋糕)
2017/10/16 全球购物
澳大利亚新奇小玩意网站:Yellow Octopus
2017/12/28 全球购物
药学专业个人的自我评价
2013/12/31 职场文书
十佳大学生村官事迹
2014/01/09 职场文书
关于赌博的检讨书
2014/01/24 职场文书
建设工程授权委托书
2014/09/22 职场文书
会议接待欢迎标语
2014/10/08 职场文书
CSS3实现的侧滑菜单
2021/04/27 HTML / CSS
Python3 类型标注支持操作
2021/06/02 Python
Golang 并发下的问题定位及解决方案
2022/03/16 Golang
vue中使用mockjs配置和使用方式
2022/04/06 Vue.js