登录  /  注册

实例详解Python数据结构之单链表

巴扎黑
发布: 2017-09-13 09:46:29
原创
993人浏览过

这篇文章主要为大家详细介绍了python数据结构之单链表的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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__==&#39;__main__&#39;:
  a=SingleLinkedList()
  for i in range(1,10):
    a.append(i)
  print(&#39;链表的大小&#39;,a.size())
  a.travel()
  print(a.search(6))
  print(a.index(5))
  a.remove(4)
  a.travel()
  a.insert(4,100)
  a.travel()
登录后复制

以上就是实例详解Python数据结构之单链表的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号