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. LaTex标准article文件框架解析

    新建一个LaTex-Article文件 生成的文件框架代码 % ---------------------------------------------------------------- % A ...

  2. Chromium之工程依赖关系.

    Chromium各版本可能有差异,我的版本是chromium.r197479,2013/08前后下载的source code. Visual Studio Ultimate版本有工具可以自动生成项目依 ...

  3. angular强制刷新

    有时候请求完毕,某些变量重新赋值后不会体现在页面上,此时需要强制刷新 $scope.$apply(function () { $scope.message ="Timeout called! ...

  4. elementUI的导航栏怎么根据路由默认选中相关项

    1. <el-menu :default-active="this.$route.path.substr(1)" class="left-nav"> ...

  5. struts标签中的select

    <!-- Struts下拉列表标签: name="deptId" 下拉列表标签的名称(服务器根据这个名称获取选择的项的实际的值value值) headerKey 默认选择项的 ...

  6. hadoop 集群常见错误解决办法

    hadoop 集群常见错误解决办法 hadoop 集群常见错误解决办法: (一)启动Hadoop集群时易出现的错误: 1.   错误现象:Java.NET.NoRouteToHostException ...

  7. [UOJ #51]【UR #4】元旦三侠的游戏

    题目大意:给$n$,一个游戏,给$a,b$,两个人,每人每次可以把$a$或$b$加一,要求$a^b\leqslant n$,无法操作人输.有$m$次询问,每次给你$a,b$,问先手可否必胜 题解:令$ ...

  8. AOJ.602 大家来找茬

    大家来找茬 Time Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 64 MB Total Submission: 627 Submiss ...

  9. 【BZOJ 1124】[POI2008] 枪战Maf Tarjan+树dp

    #define int long long using namespace std; signed main(){ 这个题一看就是图论题,然后我们观察他的性质,因为一个图论题如果没有什么性质,就是真· ...

  10. 【BZOJ 4832】 [Lydsy2017年4月月赛] 抵制克苏恩 期望概率dp

    打记录的题打多了,忘了用开维记录信息了......我们用f[i][j][l][k]表示已经完成了i次攻击,随从3血剩j个,2血剩l个,1血剩k个,这样我们求出每个状态的概率,从而求出他们对答案的贡献并 ...