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线程锁(thread)学习示例
Dec 04 Python
在Django的模板中使用认证数据的方法
Jul 23 Python
python简单分割文件的方法
Jul 30 Python
Python 实现一个颜色色值转换的小工具
Dec 06 Python
解决pandas使用read_csv()读取文件遇到的问题
Jun 15 Python
Python Pywavelet 小波阈值实例
Jan 09 Python
python 串口读取+存储+输出处理实例
Dec 26 Python
opencv python Canny边缘提取实现过程解析
Feb 03 Python
Python3标准库之threading进程中管理并发操作方法
Mar 30 Python
Python结合Window计划任务监测邮件的示例代码
Aug 05 Python
scrapy处理python爬虫调度详解
Nov 23 Python
如何用 Python 处理不平衡数据集
Jan 04 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 移除数组重复元素的一点说明
2008/11/27 PHP
php面向对象全攻略 (十七) 自动加载类
2009/09/30 PHP
PHP5权威编程阅读学习笔记 附电子书下载
2012/07/05 PHP
$_GET['goods_id']+0 的使用详解
2013/06/06 PHP
PHP curl实现抓取302跳转后页面的示例
2014/07/04 PHP
用PHP代码给图片加水印
2015/07/01 PHP
浅谈php调用python文件
2019/03/29 PHP
jquery jqPlot API 中文使用教程(非常强大的图表工具)
2011/08/15 Javascript
jquery实现邮箱自动补全功能示例分享
2014/02/17 Javascript
jquery实现点击文字可编辑并修改保存至数据库
2014/04/15 Javascript
ZeroClipboard插件实现多浏览器复制功能(支持firefox、chrome、ie6)
2014/08/30 Javascript
JS+CSS实现模仿浏览器网页字符查找功能的方法
2015/02/26 Javascript
jQuery操作iframe中js函数的方法小结
2016/07/06 Javascript
javascript获取网页各种高宽及位置的方法总结
2016/07/27 Javascript
nodejs连接mysql数据库简单封装示例-mysql模块
2017/04/10 NodeJs
Spring Boot/VUE中路由传递参数的实现代码
2018/03/02 Javascript
Vue form表单动态添加组件实战案例
2019/09/02 Javascript
Vue动态加载图片在跨域时无法显示的问题及解决方法
2020/03/10 Javascript
Vue 实现一个简单的鼠标拖拽滚动效果插件
2020/12/10 Vue.js
[33:17]OG vs VGJ.T 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
python赋值操作方法分享
2013/03/23 Python
python文件与目录操作实例详解
2016/02/22 Python
Python程序中用csv模块来操作csv文件的基本使用教程
2016/03/03 Python
使用Python微信库itchat获得好友和群组已撤回的消息
2018/06/24 Python
浅析Python 引号、注释、字符串
2019/07/25 Python
django 框架实现的用户注册、登录、退出功能示例
2019/11/28 Python
Python 实现国产SM3加密算法的示例代码
2020/09/21 Python
越南母婴用品购物网站:Kids Plaza
2020/04/09 全球购物
什么造成了Java里面的异常
2016/04/24 面试题
销售文员的岗位职责
2013/11/20 职场文书
《囚绿记》教学反思
2014/03/01 职场文书
2014年小学少先队工作总结
2014/12/18 职场文书
工人先锋号申报材料
2014/12/29 职场文书
环保主题班会教案
2015/08/13 职场文书
MySQL慢查询优化解决问题
2022/03/17 MySQL
「回转企鹅罐」10周年纪念展「輪るピングドラム展」海报公开
2022/03/22 日漫