Python 实现双向链表(图解)
原文:https://blog.csdn.net/qq490691606/article/details/49948263
git 路径 https://github.com/wangpanjun/datastructure.git
git 路径 https://github.com/wangy8961/python3-algorithms
双向链表
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点
双向链表基本方法实现(Python)
1. 初始化链表
定义节点结构:指针域pre、next和数据域data
为方便操作添加了head和tail节点,初始化时head.next–>tail,tail.pre–>next
"""节点类"""
class Node(object):
def __init__(self, data=None):
self.data = data
self.pre = None
self.next = None
"""初始化双向链表"""
def __init__(self):
"""
设置头尾,操作比较容易
头--(next)--》尾
尾--(pre)--》头
:return:
"""
head = Node()
tail = Node()
self.head = head
self.tail = tail
self.head.next = self.tail
self.tail.pre = self.head

2. 获取链表长度
起始head,每有一个节点,length+1
"""获取链表长度"""
def __len__(self):
length = 0
node = self.head
while node.next != self.tail:
length += 1
node = node.next
return length
3. 追加节点
因为有tail 节点,所以找到tail.pre 节点就好了
"""追加节点"""
def append(self, data):
"""
:param data:
:return:
"""
node = Node(data)
pre = self.tail.pre
pre.next = node
node.pre = pre
self.tail.pre = node
node.next = self.tail
return node

4. 获取节点
获取节点要判断index正负值
"""获取节点"""
def get(self, index):
"""
获取第index个值,若index>0正向获取else 反向获取
:param index:
:return:
"""
length = len(self)
index = index if index >= 0 else length + index
if index >= length or index < 0: return None
node = self.head.next
while index:
node = node.next
index -= 1
return node
5. 设置节点
找到当前节点赋值即可
"""设置节点""" def set(self, index, data):
node = self.get(index)
if node:
node.data = data
return node
6. 插入节点
插入节点需要找到插入节点的前一个节点pre_node和后一个节点next_node(索引index的正负,前一节点不同,需要判断一下),然后将pre_node.next–>node,node.pre->pre_node;next_node.pre–>node,node.next–>next_node
"""插入节点"""
def insert(self, index, data):
"""
因为加了头尾节点所以获取节点node就一定存在node.next 和 node.pre
:param index:
:param data:
:return:
"""
length = len(self)
if abs(index + 1) > length:
return False
index = index if index >= 0 else index + 1 + length
next_node = self.get(index)
if next_node:
node = Node(data)
pre_node = next_node.pre
pre_node.next = node
node.pre = pre_node
node.next = next_node
next_node.pre = node
return node

7. 删除节点
删除节点,也要区分一下索引的正负。找到当前节点的前一个节点pre_node和后一个节点next_node,将pre_node.nex–>next_node即可
"""删除节点"""
def delete(self, index):
node = self.get(index)
if node:
node.pre.next = node.next
node.next.pre = node.pre
return True
return False

8. 反转链表
反转链表的实现有多种方式,比较简单的就是生成一个新的链表--》可以用数组存储所有节点让后倒序生成新的链表
在这里用下面这种方式生产:
可能有点绕
1.node.next –> node.pre;node.pre –> node.next(递归)
2.head.next –> None;tail.pre –> None
3.head–>tail;tail–>head
"""反转链表"""
def __reversed__(self):
"""
1.node.next --> node.pre
node.pre --> node.next
2.head.next --> None
tail.pre --> None
3.head-->tail
tail-->head
:return:
"""
pre_head = self.head
tail = self.tail
def reverse(pre_node, node):
if node:
next_node = node.next
node.next = pre_node
pre_node.pre = node
if pre_node is self.head:
pre_node.next = None
if node is self.tail:
node.pre = None
return reverse(node, next_node)
else:
self.head = tail
self.tail = pre_head
return reverse(self.head, self.head.next)

9. 清空链表
类似初始化
"""清空链表"""
def clear(self):
self.head.next = self.tail
self.tail.pre = self.head

Python 实现双向链表(图解)的更多相关文章
- (转)Python 实现双向链表(图解)
原文:https://blog.csdn.net/qq490691606/article/details/49948263 Python 实现双向链表(图解)双向链表双向链表也叫双链表,是链表的一种, ...
- Cobra —— 可视化Python虚拟机 and 图解python
http://blog.csdn.net/balabalamerobert http://blog.csdn.net/efeics/article/category/1486515 图解python ...
- Python的安装图解
安装步骤: 第一步:打开Python官网:http://www.python.org 第二步:点击Download,下载windows版本 第三步:选择要下载的版本第四步:安装到指定的位置第五步:验证 ...
- python实现双向链表的操作
双向链表 双向链表又叫做双链表,每个节点有两个指针域和一个数据域.prev指针域指向前一个节点,next指针域指向下一个节点.注意,第一个节点的prev指针域指向空值,最后一个节点的next域也是指向 ...
- Robot framework+python安装使用图解版
一.安装包 1.Python2.7(一切的基础,切记安装目录不能有中文不能有空格) 1)python2.7:(python环境):python-2.7.msi 2)setuptools(python包 ...
- python实现双向链表
双向链表 一种更复杂的链表是“双向链表”或“双面链表”.每个节点有两个链接:一个指向前一个节点,当此节点为第一个节点时,指向空值:而另一个指向下一个节点,当此节点为最后一个节点时,指向空值. 实现 c ...
- Python数据结构--双向链表
''' 双向链表包含第一个和最后一个的链接元素. 每个链接都有一个数据字段和两个称为next和prev的链接字段. 每个链接都使用其下一个链接与其下一个链接链接. 每个链接都使用其上一个链接与之前的链 ...
- 数据结构与算法-python描述-双向链表
# coding:utf-8 # 双向链表的相关操作: # is_empty() 链表是否为空 # length() 链表长度 # travel() 遍历链表 # add(item) 链表头部添加 # ...
- Python的双向链表实现
思路 链表由节点组成,先规定节点(Node),包含data和指向下个节点的next 初始化 data当然就是传入的data了,next和prev指向None 添加 分两种情况: 链表为空,那么头节点和 ...
随机推荐
- nwjs问题总结
1.iframe中不支持flash解决方法: nw初始化中加入代码: // 设置flashplayer在iframe中可用 chrome.contentSettings.plugins.set({ p ...
- WebGL编程指南案例解析之平移和旋转的math库实现
这里说的math库实现,指的是,通过一般的加减乘除(角度计算)来更新坐标值. 因为涉及到坐标的变化,所以这里都是基于对顶点着色器的修改 平移: var vShader = ` attribute ve ...
- Foundation--NSString , array and Dictionary
一,NSString的创建 NSString*str=@" a string ";//直接创建一个字符串常量,这样创建出来的字符串不需要释放内存 NSLog(@"%@&q ...
- ubuntu下设置电脑为WiFi热点
这是个问题 自从Windows奔溃以来! 扔掉360:Linux下无线网卡作WiFi路由器
- Spring AOP关于cglib动态代理
一: Spring AOP的默认代理方式是jdk动态代理,还有另外一种代理方式是cglib代理,简单说前者基于接口,后者基于继承,基本思路是将被代理对象的类作为父类,然后创建子类来进行方法的调用,调用 ...
- 如何在Struts2的拦截器中调用Spring容器
第一种: 通常用ApplicationContext来调用Spring配置文件中的一些Bean,所以首先创建Spring上下文容器. ApplicationContext ac = (Applicat ...
- Heap Operations 优先队列
Petya has recently learned data structure named "Binary heap". The heap he is now operatin ...
- day3 自动部署安装软件到其他的机器设备上
PS:原理是在本机创建boot.sh指向每一台主机,使用脚本命令去执行,然后就会自动安装软件 PS:boot.sh里面放着1.免密登录 2.发送每台机器install.sh 这个install.sh中 ...
- Array.prototype.slice.call(arguments)探究
Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组 首先,slice有两个用法,一个是String.slice,一个是Array.slic ...
- android 发送UDP广播,搜寻server建立socket链接
应用场景:client(手机.pc)须要搜寻所在局域网内的server并获得server地址. 方法简单介绍:client发送UDP广播,服务收到广播后得到clientip地址,然后向client发送 ...