Python数据结构之单链表详解


Posted in Python onSeptember 12, 2017

本文实例为大家分享了Python数据结构之单链表的具体代码,供大家参考,具体内容如下

# 节点类
class Node():
  __slots__=['_item','_next'] # 限定Node实例的属性
  def __init__(self,item):
    self._item = item
    self._next = None # Node的指针部分默认指向None
  def getItem(self):
    return self._item
  def getNext(self):
    return self._next
  def setItem(self,newitem):
    self._item = newitem
  def setNext(self,newnext):
    self._next=newnext

# 单链表
class SingleLinkedList():
  def __init__(self):
    self._head = None #初始化链表为空 始终指向链表的头部
    self._size = 0 # 链表大小

  # 返回链表的大小
  def size(self):
    current = self._head
    count = 0
    while current != None:
      count += 1
      current = current.getNext()
    return count

  # 遍历链表
  def travel(self):
    current = self._head
    while current != None:
      print(current.getItem())
      current = current.getNext()
  # 检查链表是否为空
  def isEmpty(self):
    return self._head == None

  # 在链表前端添加元素
  def add(self,item):
    temp = Node(item) # 创建新的节点
    temp.setNext(self._head) # 新创建的next指针指向_head
    self._head = temp # _head指向新创建的指针

  # 在链表尾部添加元素
  def append(self,item):
    temp = Node(item)
    if self.isEmpty():
      self._head = temp # 若为空表就直接插入
    else:
      current = self._head
      while current.getNext() != None:
        current = current.getNext() # 遍历列表
      current.setNext(temp) # 此时current为链表最后的元素,在末尾插入

  # 检索元素是否在链表中
  def search(self,item):
    current = self._head
    founditem = False
    while current != None and not founditem:
      if current.getItem() == item:
        founditem = True
      else:
        current = current.getNext()
    return founditem

  # 索引元素在表中的位置
  def index(self,item):
    current = self._head
    count = 0
    found = None
    while current != None and not found:
      count += 1
      if current.getItem() == item:
        found = True
      else:
        current = current.getNext()
    if found:
      return count
    else:
      return -1 # 返回-1表示不存在

  # 删除表中的某项元素
  def remove(self,item):
    current = self._head
    pre = None
    while current!=None:
      if current.getItem() == item:
        if not pre:
          self._head = current.getNext()
        else:
          pre.setNext(current.getNext())
        break
      else:
        pre = current
        current = current.getNext()

  # 在链表任意位置插入元素
  def insert(self,pos,item):
    if pos <= 1:
      self.add(item)
    elif pos > self.size():
      self.append(item)
    else:
      temp = Node(item)
      count = 1
      pre = None
      current = self._head
      while count < pos:
        count += 1
        pre = current
        current = current.getNext()
      pre.setNext(temp)
      temp.setNext(current)


if __name__=='__main__':
  a=SingleLinkedList()
  for i in range(1,10):
    a.append(i)
  print('链表的大小',a.size())
  a.travel()
  print(a.search(6))
  print(a.index(5))
  a.remove(4)
  a.travel()
  a.insert(4,100)
  a.travel()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中random模块用法实例分析
May 19 Python
python+opencv识别图片中的圆形
Mar 25 Python
Anaconda2下实现Python2.7和Python3.5的共存方法
Jun 11 Python
解决PyCharm同目录下导入模块会报错的问题
Oct 13 Python
用python 实现在不确定行数情况下多行输入方法
Jan 28 Python
Python实现监控Nginx配置文件的不同并发送邮件报警功能示例
Feb 26 Python
详解Python中的内建函数,可迭代对象,迭代器
Apr 29 Python
python 实现在tkinter中动态显示label图片的方法
Jun 13 Python
Django用户认证系统 组与权限解析
Aug 02 Python
解决python Jupyter不能导入外部包问题
Apr 15 Python
通过Python扫描代码关键字并进行预警的实现方法
May 24 Python
在python中使用pyspark读写Hive数据操作
Jun 06 Python
python处理Excel xlrd的简单使用
Sep 12 #Python
Python3.6简单操作Mysql数据库
Sep 12 #Python
Python文件和流(实例讲解)
Sep 12 #Python
Anaconda多环境多版本python配置操作方法
Sep 12 #Python
python 随机数使用方法,推导以及字符串,双色球小程序实例
Sep 12 #Python
python监控linux内存并写入mongodb(推荐)
Sep 11 #Python
python学习教程之Numpy和Pandas的使用
Sep 11 #Python
You might like
一个php作的文本留言本的例子(五)
2006/10/09 PHP
PHP和JAVA中的重载(overload)和覆盖(override) 介绍
2012/03/01 PHP
解决FastCGI 进程超过了配置的活动超时时限的问题
2013/07/03 PHP
PHP统计二维数组元素个数的方法
2013/11/12 PHP
php获取客户端电脑屏幕参数的方法
2015/01/09 PHP
php获取文件名后缀常用方法小结
2015/02/24 PHP
浅析ThinkPHP缓存之快速缓存(F方法)和动态缓存(S方法)(日常整理)
2015/10/26 PHP
分享php多功能图片处理类
2016/05/15 PHP
javascript 最常用的10个自定义函数[推荐]
2009/12/26 Javascript
javascript实现动态改变层大小的方法
2015/05/14 Javascript
js实现简单计算器
2015/11/22 Javascript
浅析Bootstrip的select控件绑定数据的问题
2016/05/10 Javascript
JavaScript函数中关于valueOf和toString的理解
2016/06/14 Javascript
jquery实现网站列表切换效果的2种方法
2016/08/12 Javascript
浅谈vue中慎用style的scoped属性
2017/11/28 Javascript
vue实现2048小游戏功能思路详解
2018/05/09 Javascript
vue spa应用中的路由缓存问题与解决方案
2019/05/31 Javascript
python 实现堆排序算法代码
2012/06/05 Python
Python解决鸡兔同笼问题的方法
2014/12/20 Python
Python环境下搭建属于自己的pip源的教程
2016/05/05 Python
Python实现excel转sqlite的方法
2017/07/17 Python
python 读取txt,json和hdf5文件的实例
2018/06/05 Python
在Python运行时动态查看进程内部信息的方法
2019/02/22 Python
PyTorch搭建多项式回归模型(三)
2019/05/22 Python
PyCharm如何导入python项目的方法
2020/02/06 Python
Django多个app urls配置代码实例
2020/11/26 Python
Jmeter调用Python脚本实现参数互相传递的实现
2021/01/22 Python
css3实现3D文本悬停改变效果的示例代码
2019/01/16 HTML / CSS
俄罗斯购买自行车网站:Vamvelosiped
2021/01/29 全球购物
构造方法和其他方法的区别
2016/04/26 面试题
电子商务专业个人的自我评价
2013/12/19 职场文书
生日邀请函范文
2014/01/13 职场文书
英语专业学生个人求职信
2014/01/28 职场文书
大学生应聘求职信
2014/05/26 职场文书
计算机软件专业求职信
2014/06/10 职场文书
简单租房协议书
2014/10/21 职场文书