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. Mininet实验 动态改变转发规则

    介绍 拓扑如下: 在该环境下,假设H1 ping H4,初始的路由规则是S1-S2-S5,一秒后,路由转发规则变为S1-S3-S5,再过一秒,规则变为S1-S4-S5,然后再回到最初的转发规则S1-S ...

  2. Nginx 学习笔记之安装篇

    在windows下安装Nginx其实非常简单,只需如下几个步骤: 1. 在Nginx官网下载相应版本的安装程序,上面有最新版.稳定版等各种版本,正式运营的项目建议下载最新的稳定版 2.将下载后的压缩包 ...

  3. SQL SERVER 实用命令集锦

    1.根据关键字查询库中的存储过程,返回符合条件的存储过程名称 select distinct object_name(id) from syscomments where id in (select ...

  4. Mac Java配置JAVA——HOME

    命令行中输入:  export JAVA_HOME=$(/usr/libexec/java_home) 

  5. JavaScript的lazyload延迟加载是如何实现的

    懒加载技术(简称lazyload)并不是新技术, 它是js程序员对网页性能优化的一种方案.lazyload的核心是按需加载.在大型网站中都有lazyload的身影,例如谷歌的图片搜索页,迅雷首页,淘宝 ...

  6. Linux上 Can't connect to X11 window server using XX as the value of the DISPLAY 错误解决方法

    在Linux上运行需要图形界面的程序时出现如下错误提示: No protocol specified Exception in thread "main" java.awt.AWT ...

  7. MySQL主主搭建

    1.在MySQL主从的基础上修改: #master1上 [mysqld] server-id=101 log-bin = mysql-bin auto-increment-increment = 2 ...

  8. 【转载】深入理解PHP Opcode缓存原理

    转载地址:深入理解PHP Opcode缓存原理 什么是opcode缓存? 当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode).O ...

  9. Java面向对象编程三大特性 --- 多态

    多态特性: 子类Child继承父类Father,我们可以编写一个指向子类的父类类型引用,该引用既可以处理父类Father对象,也可以处理子类Child对象,当相同的消息发送给子类或者父类对象时,该对象 ...

  10. HDFS之FileSystem

    package cn.hx.test; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; impo ...