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. unity 归纳

    1.获取控件四个角在屏幕上的坐标 Vector3[] corners = new Vector3[]; gameObject.GetComponent<RectTransform>().G ...

  2. Ext.Net学习网站

    1.http://ext.net/ 官网.里面的examples是宝贝. 2.http://www.qeefee.com/zt-extnet 起飞网

  3. 制作用于日期时间型字段的DELPHI数据感知控件

    用DELPHI开发C/S应用方便而快速,因为它拥有大量易于使用的数据访问和数据感知控件.然而万事总是难以完美,DELPHI的DBEdit控件用于输入日期时间型字段却很不方便,为了改善这一缺点,笔者开发 ...

  4. 使用XML传递数据

    HTML <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  5. poj 2226 Muddy Fields (二分匹配)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7340   Accepted: 2715 Desc ...

  6. [SDOI2011]消防/[NOIP2007] 树网的核

    消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家最兴旺的 ...

  7. PHP正则匹配与替换的简单例子

    PHP正则匹配与替换的简单例子,含一个匹配获取加租字体例子和一个匹配替换超链接的例子. 1.查找匹配 <b> 与 </b> 标签的内容: <?php $str = &qu ...

  8. ppk和pem证书互转

    首先你得去下载个putty pem:通用证书格式 ppk:为putty下面的专有格式     pem->ppk 直接通过putty下的puttygen.exe 选的Load private ke ...

  9. 根据select创建input并赋值

    <!DOCTYPE html><html> <head>        <meta charset="UTF-8">         ...

  10. WebKit阅读起步

    转摘自:http://my.oschina.net/myemptybottle/blog/42683 部分转摘,全文请查看原文! 我第一次看到WebKit代码中did,will前缀有点困惑,看多了才熟 ...