原文: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