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 zip文件 压缩
Dec 24 Python
Python多线程编程(四):使用Lock互斥锁
Apr 05 Python
python获取一组汉字拼音首字母的方法
Jul 01 Python
python中正则的使用指南
Dec 04 Python
python使用fcntl模块实现程序加锁功能示例
Jun 23 Python
Python列表list内建函数用法实例分析【insert、remove、index、pop等】
Jul 24 Python
python 自定义对象的打印方法
Jan 12 Python
12个步骤教你理解Python装饰器
Jul 01 Python
Django MEDIA的配置及用法详解
Jul 25 Python
python RC4加密操作示例【测试可用】
Sep 26 Python
django 中使用DateTime常用的时间查询方式
Dec 03 Python
python的列表生成式,生成器和generator对象你了解吗
Mar 16 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
用windows下编译过的eAccelerator for PHP 5.1.6实现php加速的使用方法
2007/09/30 PHP
php数组函数序列之array_key_exists() - 查找数组键名是否存在
2011/10/29 PHP
利用php抓取蜘蛛爬虫痕迹的示例代码
2016/09/30 PHP
PHP之将POST数据转化为字符串的实现代码
2016/11/03 PHP
让textarea控件的滚动条怎是位与最下方
2007/04/20 Javascript
setInterval计时器不准的问题解决方法
2014/05/08 Javascript
jQuery实现判断滚动条到底部
2015/06/23 Javascript
深入剖析JavaScript中的函数currying柯里化
2016/04/29 Javascript
AngularJS辅助库browserTrigger用法示例
2016/11/03 Javascript
vue组件中点击按钮后修改输入框的状态实例代码
2017/04/14 Javascript
Angular使用 ng-img-max 调整浏览器中的图片的示例代码
2017/08/17 Javascript
Vue中自定义全局组件的实现方法
2017/12/08 Javascript
让axios发送表单请求形式的键值对post数据的实例
2018/08/11 Javascript
详解处理Vue单页面应用SEO的另一种思路
2018/11/09 Javascript
Node.js 进程平滑离场剖析小结
2019/01/24 Javascript
VUE.js实现动态设置输入框disabled属性
2019/10/28 Javascript
vuecli3.x中轻松4步带你使用tinymce的步骤
2020/06/25 Javascript
详解在Python程序中自定义异常的方法
2015/10/16 Python
Python中的复制操作及copy模块中的浅拷贝与深拷贝方法
2016/07/02 Python
Tensorflow 实现修改张量特定元素的值方法
2018/07/30 Python
python里运用私有属性和方法总结
2019/07/08 Python
Django 权限认证(根据不同的用户,设置不同的显示和访问权限)
2019/07/24 Python
pytest中文文档之编写断言
2019/09/12 Python
如何提高python 中for循环的效率
2020/04/15 Python
python opencv 实现读取、显示、写入图像的方法
2020/06/08 Python
PyCharm常用配置和常用插件(小结)
2021/02/06 Python
酒店七夕情人节活动策划方案
2014/08/24 职场文书
2014年除四害工作总结
2014/12/06 职场文书
党性分析材料格式
2014/12/19 职场文书
二年级学生期末评语
2014/12/26 职场文书
软件项目经理岗位职责
2015/04/01 职场文书
创业计划书之情侣餐厅
2019/09/29 职场文书
导游词之峨眉山
2019/12/16 职场文书
nginx location中多个if里面proxy_pass的方法
2021/03/31 Servers
2022新作动画《福星小子》释出宣传影片 加入内田真礼&宫野真守配音演出
2022/04/08 日漫
Python函数对象与闭包函数
2022/04/13 Python