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. Spring定时器调用Hibernate方法无法获得SessionFactory的解决办法

    由于在Spring定时器中无法通过注解的方式获取bean,因此需要通过原生的方式获取.获取session的方式如下: WebApplicationContext wac = ContextLoader ...

  2. JConsole本地连接失败

    一.问题描述 笔者在使用JDK自带的JConsole小工具连接Myeclipse里面注册的MBean时,报如下错: 二.解决办法 添加JVM运行参数: -Dcom.sun.management.jmx ...

  3. typescript 简版跳一跳

    typescript 简版跳一跳 学习typescript,第一步应该是学习官方文档,理解最基础的语法.第二步开始用typescript实现一些js+css 或者canvas类型的游行.现在开始我们用 ...

  4. 【题解】JLOI2013卡牌游戏

    这题最开始是用 \(n^{4}\)的算法水过的,之后才想出的\(n^{3}\)正解.首先,\(n^{4}\) 应该是很容易想到的:设状态 \(f[i][j][k]\) 为有 \(i\) 个人,庄家为 ...

  5. BZOJ1305 [CQOI2009]dance跳舞 【网络流】

    1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 3714  Solved: 1572 [Submit][ ...

  6. Link Cat Tree (连喵树) 学习笔记

    Link Cat Tree 一.感性定义 所谓连喵树,即一种对森林支持修改,查询,连边,删边等操作的数据结构(姑且算她是吧).她用一颗颗互相连接的辅助树维护原森林的信息,辅助树相互连接的边叫虚边,辅助 ...

  7. 面试前需要弄懂的SQL

    说明:创建数据库 view source   print? 1 Create DATABASE database-name 说明:删除数据库 view source   print? 1 drop d ...

  8. CentOS 7, Attempting to create directory /root/perl5

    By francis_hao    Apr 10,2017 在使用CentOS 7的时候,首次登陆会出现新建一个perl5文件夹的提示,删除该文件后,之后登陆还是会出现该提示并新建了perl5文件夹. ...

  9. MySQL的字符集小结

    正确了解MySQL的字符集问题,能够从根本上解决乱码的困扰. 首先,MySQL的字符集问题主要是两个概念,一个是Character Sets,一个是Collations,前者是字符内容及编码,后者是对 ...

  10. Dilworth定理证明

    命题:偏序集能划分成的最少的全序集的个数与最大反链的元素个数相等. (离散数学结构第六版课本P245:把一个偏序集划分成具有全序的子集所需要的最少子集个数与元素在偏序下都是不可比的最大集合的基数之间有 ...