python实现数据结构中双向循环链表操作的示例

发布时间:2020-10-24 11:39:27 作者:GUET_DM_WLB
来源:脚本之家 阅读:148

看此博客之前建议先看看B站的视频python数据结构与算法系列课程,该课程中未实现双向循环链表的操作,所以我按照该视频的链表思路实现了双向循环链表的操作,欢迎大家阅读与交流,如有侵权,请联系博主!

下面附上代码:

class Node:
  def __init__(self, elem):
    self.elem = elem
    self.prev = None
    self.next = None


class DoubleCycleLinkList:
  def __init__(self, node=None):
    self.__head = node

  def is_empty(self):
    """判空"""
    if self.__head is None:
      return True
    return False

  def length(self):
    """链表长度"""
    if self.is_empty():
      return 0
    cur = self.__head
    count = 1
    while cur.next is not self.__head:
      count += 1
      cur = cur.next
    return count

  def travel(self):
    """遍历链表"""
    if self.is_empty():
      return
    cur = self.__head
    while cur.next is not self.__head:
      print(cur.elem, end=" ")
      cur = cur.next
    print(cur.elem, end=" ")
    print("")

  def add(self, elem):
    """头插法"""
    node = Node(elem)
    if self.is_empty():
      self.__head = node
      node.prev = node
      node.next = node
    else:
      self.__head.prev.next = node
      node.prev = self.__head.prev
      node.next = self.__head
      self.__head.prev = node
      self.__head = node

  def append(self, elem):
    """尾插法"""
    node = Node(elem)
    if self.is_empty():
      self.__head = node
      node.prev = node
      node.next = node
    else:
      node.next = self.__head
      node.prev = self.__head.prev
      self.__head.prev.next = node
      self.__head.prev = node

  def insert(self, pos, elem):
    """任一位置(pos)插入, 下标从0数起"""
    if pos <= 0:
      self.add(elem)
    elif pos > (self.length() - 1):
      self.append(elem)
    else:
      count = 0
      cur = self.__head
      node = Node(elem)
      while count < (pos - 1):
        count += 1
        cur = cur.next
      node.next = cur.next
      node.prev = cur
      node.next.prev = node
      cur.next = node

  def remove(self, elem):
    """删除某一节点,若有多个符合条件的节点,删除第一个即可"""
    if self.is_empty():
      return
    cur = self.__head
    while cur.next is not self.__head:
      if cur.elem == elem:
        if cur is self.__head:
          self.__head = cur.next
          cur.prev.next = cur.next
          cur.next.prev = cur.prev
        else:
          cur.prev.next = cur.next
          cur.next.prev = cur.prev
        break
      cur = cur.next
    if cur.elem == elem:
      cur.prev.next = self.__head
      self.head = cur.prev

  def search(self, elem):
    """查找某一个节点"""
    if self.is_empty():
      return False
    cur = self.__head
    while cur.next is not self.__head:
      if cur.elem == elem:
        return True
      cur = cur.next
    # while中处理不到尾节点,所以进行最后尾节点的判断
    if cur.elem == elem:
      return True
    return False

到此这篇关于python实现数据结构中双向循环链表操作的示例的文章就介绍到这了,更多相关python 双向循环链表操作内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

推荐阅读:
  1. 数据结构-链表的操作
  2. Python数据结构dict常用操作代码的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 双向循环链表

上一篇:Spring Cloud基于zuul实现网关过程解析

下一篇:JavaWeb禁止浏览器缓存当前Web页面的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》