LinkedList是一个双向线性链表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而顺序表相应的时间复杂度分别是O(logn)和O(1)。

首先,我们来实现一个Node,看代码:

class Node(object):
def __init__(self,value,prev_Node=None,next_Node=None):
self.value=value
self.prev_node=prev_Node
self.next_node=next_Node def get_prev_node(self):
return self._prev_node def set_prev_node(self,prev_Node):
self._prev_node=prev_Node def del_prev_node(self):
del self._prev_node prev_node=property(get_prev_node,set_prev_node,del_prev_node) def get_next_node(self):
return self._next_node def set_next_node(self,next_Node):
self._next_node=next_Node def del_next_node(self):
del self._next_Node next_Node=property(get_next_node,set_next_node,del_next_node) def get_value(self):
return self._value def set_value(self,value):
self._value=value def del_value(self):
del self._value value=property(get_value,set_value,del_value)

然后,生成LinkedList:

class LinkedList(object):
# comparator is a function used by LinkedList to compare nodes
# it's expected to take two parameters:
# it returns 0 if both parameters are equal, 1 if the left parameter is greater, and -1 if the lft parameter is lesser
def __init__(self, comparator):
self.head = None
self.comparator = comparator # Adds a value to the LinkedList while maintaining a sorted state
def insert(self, value):
node = Node(value) # If the linked list is empty, make this the head node
if self.head is None:
self.head = node
# Otherwise, insert the node into the sorted linked list
else:
curr_node = self.head
b_node_not_added = True
while b_node_not_added:
result = self.comparator(node.value, curr_node.value) # Store the next and previous node for readability
prev = curr_node.prev_node
next = curr_node.next_node # If the current node is greater, then insert this node into its spot
if result < 0:
# If the curr_node was the head, replace it with this node
if self.head == curr_node:
self.head = node
# Otherwise, it has a previous node so hook it up to this node
else:
node.prev_node = prev
prev.next_node = node # Hook the current node up to this node
node.next_node = curr_node
curr_node.prev_node = node b_node_not_added = False
# Otherwise, continue traversing
else:
# If we haven't reached the end of the list, keep traversing
if next is not None:
curr_node = next
# Otherwise, append this node
else:
curr_node.next_node = node
node.prev_node = curr_node b_node_not_added = False def remove(self, value):
curr_node = self.head while curr_node is not None:
# Store the current node's neighbors for readability
prev = curr_node.prev_node
next = curr_node.next_node # Check if this is the node we're looking for
result = self.comparator(value, curr_node.value) # If it's equal, then remove the current node
b_node_is_equal = result == 0
if b_node_is_equal:
# If the removed node is the head node, re-assign the head node
if self.head == curr_node:
self.head = next
# Otherwise, remove the node normally
else:
if prev is not None:
prev.next_node = next
if next is not None:
next.prev_node = prev curr_node = None
# Otherwise, continue traversing the list
else:
curr_node = next # Print out the contents of the linked list
def print(self):
curr_node = self.head
while curr_node is not None:
print(curr_node.value)
curr_node = curr_node.next_node

python算法:LinkedList(双向线性链表)的实现的更多相关文章

  1. 【C++】双向线性链表容器的实现

    // 双向线性链表容器 #include <cstring> #include <iostream> #include <stdexcept> using name ...

  2. 【二叉树->链表】二叉树结构转双向线性链表结构(先序遍历)

    二叉树存储结构属于非线性链表结构,转化成线性链表结构,能简化操作和理解.然而由非线性转线性需要对整个树遍历一次,不同的遍历方式转化结果页不一样.下面以先序为例. 方法一: 递归法.递归遍历二叉树,因为 ...

  3. python算法与数据结构-单链表(38)

    一.链表 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点包括 ...

  4. JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)

    前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...

  5. 数据结构算法C语言实现(五)---2.3重新定义线性链表及其基本操作

    一.简述 ...由于链表在空间的合理利用上和插入.删除时不需要移动等的优点,因此在很多场合下,它是线性表的首选存储结构.然而,它也存在着实现某些基本操作,如求线性表的长度时不如顺序存储结构的缺点:另一 ...

  6. javascript实现数据结构与算法系列:功能完整的线性链表

    由于链表在空间的合理利用上和插入,删除时不需要移动等的有点,因此在很多场合下,它是线性表的首选存储结构.然而,它也存在着实现某些基本操作,如求线性表长度时不如顺序存储结构的缺点:另一方面,由于在链表中 ...

  7. javascript实现数据结构与算法系列:线性表的静态单链表存储结构

    有时可借用一维数组来描述线性链表,这就是线性表的静态单链表存储结构. 在静态链表中,数组的一个分量表示一个结点,同时用游标(cur)代替指针指示结点在数组中的相对位置.数组的第0分量可看成头结点,其指 ...

  8. 数据结构与算法之美 06 | 链表(上)-如何实现LRU缓存淘汰算法

    常见的缓存淘汰策略: 先进先出 FIFO 最少使用LFU(Least Frequently Used) 最近最少使用 LRU(Least Recently Used) 链表定义: 链表也是线性表的一种 ...

  9. Java && Python 算法面试常用类以及方法总结

    数据结构 逻辑结构上: 包括集合,线性结构,非线性结构. 存储结构: 顺序存储,链式存储,索引存储,散列存储. Java 常见数据结构 大专栏  Java && Python 算法面试 ...

随机推荐

  1. SDUST OJ Problem G 动态的字符串排序

    Description 把字符串按照ASCII码序的从小到大排列出来. 串的ASCII码序遵循如下递归定义: 1 两串的前n-1个字符相同,第n个字符ASCII码序小的排在前面:2 只有两串的字符完全 ...

  2. multi-tap

    multi-tap又称 multi-press . 是在手机,或者电视遥控上的keypad定义,有如下2类标准: 1. ITU-T E.161 2.T9 使用举例如下: Consider a typi ...

  3. 关于php网络爬虫phpspider

    前几天,被老板拉去说要我去抓取大众点评某家店的数据,当然被我义正言辞的拒绝了,理由是我不会...但我的反抗并没有什么卵用,所以还是乖乖去查资料,因为我是从事php工作的,首先找的就是php的网络爬虫源 ...

  4. 【题解】SDOI2010地精部落

    强!强!强!强!劲啊劲啊劲啊!!!洛谷P2467 非常重要的,就在于发现以下的两条性质: 1.当i与i+1不相邻时,方案数是一样的:交换这两个数,<i+1的必然<i,>i+1的必然& ...

  5. BZOJ4012 [HNOI2015]开店 【动态点分治 + splay】

    题目链接 BZOJ4012 题解 Mychael并没有A掉,而是T掉了 讲讲主要思路 在点分树上每个点开两棵\(splay\), 平衡树\(A\)维护子树中各年龄到根的距离 平衡树\(B\)维护子树中 ...

  6. 安徽师大附中%你赛day7 T2 乘积 解题报告

    乘积 题目背景 \(\mathrm{Smart}\) 最近在潜心研究数学, 他发现了一类很有趣的数字, 叫做无平方因子数. 也就是这一类数字不能够被任意一个质数的平方整除, 比如\(6\).\(7\) ...

  7. [Noip2004]虫食算 dfs

    搜索问题的关键:优秀的搜索策略以及行之有效的减枝 对于这道题我们阶乘搜肯定不行所以我们按位搜,我们对每一位的三个数进行赋值,然后判解. 对于此一类的搜索乘上一个几十的常数来减枝往往要比直接搜要快得多, ...

  8. [COGS 622] [NOIP2011] 玛雅游戏 模拟

    整个模拟的关键除了打出来就是一个剪枝:对于两个左右相邻的块你不用再走←,因为走→是等效的 #include<cstdio> #include<cstring> #include ...

  9. 构建一个类jq的函数库

    jqfree core var $ = function(selector, context) { return new $.fn.init(selector, context); }; $.fn = ...

  10. centos7 mysql cluster集群搭建基于docker

    1.准备 mn:集群管理服务器用于管理集群的其他节点.我们可以从管理节点创建和配置集群上的新节点.重新启动.删除或备份节点. db2/db3:这是节点间同步和数据复制的过程发生的层. db4/db5: ...