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


Posted in Python onOctober 09, 2020

看此博客之前建议先看看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 双向循环链表操作内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python通过装饰器检查函数参数数据类型的方法
Mar 13 Python
python中global用法实例分析
Apr 30 Python
Python实现动态图解析、合成与倒放
Jan 18 Python
Flask之flask-session的具体使用
Jul 26 Python
Python之inspect模块实现获取加载模块路径的方法
Oct 16 Python
如何使用Python自动控制windows桌面
Jul 11 Python
pyenv与virtualenv安装实现python多版本多项目管理
Aug 17 Python
python 求定积分和不定积分示例
Nov 20 Python
Python箱型图处理离群点的例子
Dec 09 Python
Python加密模块的hashlib,hmac模块使用解析
Jan 02 Python
python使用yaml 管理selenium元素的示例
Dec 01 Python
Python&Matlab实现灰狼优化算法的示例代码
Mar 21 Python
Python collections模块的使用方法
Oct 09 #Python
python爬取代理IP并进行有效的IP测试实现
Oct 09 #Python
Python中Selenium模块的使用详解
Oct 09 #Python
python利用platform模块获取系统信息
Oct 09 #Python
python smtplib发送多个email联系人的实现
Oct 09 #Python
python 决策树算法的实现
Oct 09 #Python
Python+unittest+requests 接口自动化测试框架搭建教程
Oct 09 #Python
You might like
无线电广播与收音机发展的历史回眸
2021/03/02 无线电
php 正则匹配函数体
2009/08/25 PHP
php实现比较两个文件夹异同的方法
2015/06/18 PHP
PHP微信开发之微信消息自动回复下所遇到的坑
2016/05/09 PHP
php in_array() 检查数组中是否存在某个值详解
2016/11/23 PHP
在laravel中使用with实现动态添加where条件
2019/10/10 PHP
jQuery ajax cache缓存问题
2010/07/01 Javascript
jquery中获取元素的几种方式小结
2011/07/05 Javascript
jquery通过closest选择器修改上级元素的方法
2015/03/17 Javascript
js实现根据身份证号自动生成出生日期
2015/12/15 Javascript
一种基于浏览器的自动小票机打印实现方案(js版)
2016/07/26 Javascript
本地Bootstrap文件字体图标引入却无法显示问题的解决方法
2020/04/18 Javascript
用file标签实现多图文件上传预览
2017/02/14 Javascript
Angular2 Service实现简单音乐播放器服务
2017/02/24 Javascript
vue js秒转天数小时分钟秒的实例代码
2018/08/08 Javascript
Nodejs核心模块之net和http的使用详解
2019/04/02 NodeJs
通过实例解析vuejs如何实现调试代码
2020/07/16 Javascript
微信小程序实现页面左右滑动
2020/11/16 Javascript
Python 2.7.x 和 3.x 版本的重要区别小结
2014/11/28 Python
python使用多线程不断刷新网页的方法
2015/03/31 Python
Python里disconnect UDP套接字的方法
2015/04/23 Python
在Pycharm中对代码进行注释和缩进的方法详解
2019/01/20 Python
python买卖股票的最佳时机(基于贪心/蛮力算法)
2019/07/05 Python
python使用flask与js进行前后台交互的例子
2019/07/19 Python
PyQt+socket实现远程操作服务器的方法示例
2019/08/22 Python
Python Lambda函数使用总结详解
2019/12/11 Python
tensorflow 查看梯度方式
2020/02/04 Python
python3中确保枚举值代码分析
2020/12/02 Python
Python的信号库Blinker用法详解
2020/12/31 Python
CSS3条纹背景制作的实战攻略
2016/05/31 HTML / CSS
新秀丽拉杆箱美国官方网站:Samsonite美国
2016/07/25 全球购物
打架检讨书500字
2014/01/29 职场文书
小摄影师教学反思
2014/04/27 职场文书
2015年银行大堂经理工作总结
2015/04/24 职场文书
php字符串倒叙
2021/04/01 PHP
win10拖拽文件时崩溃怎么解决?win10文件不能拖拽问题解决方法
2022/08/14 数码科技