linked list(链表) 建立

Node

链表的基本组成就是一个个Node,每个Node都需要包括两部分内容,一部分是自身的data,另一部分是下一个Node的reference。

class Node:
def __init__(self, init_data):
self.data = init_data
self.next = None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_data(self, new_data):
self.data = newdata
def set_next(self, new_next):
self.next = new_next

每一个链表都有一个Head和一个End结点,End结点的nextNone

无序链表

this list is unordered, the specific location of the new item with respect to the other items already in the list is not important. The new item can go anywhere.

add操作

python链表增加新结点的过程我觉得跟我用C语言写还是有差别的。

整个过程如图,增加的结点位于链表的头部,也就是说,向前增加新结点:

  1. 创建新结点
  2. 将新结点的next指向旧链表的head
  3. 将新链表的head设为新结点

‘遍历’操作(traverse)

get_next()方法是该操作中主要用到的函数。

size, search, remove 都是基于‘遍历’操作

size操作

  1. 找到head
  2. 沿着head一个个摸下去,摸一个count+1

search操作

  1. 找到head
  2. 沿着head一个个摸下去,摸到item返回True,摸不到继续往下摸

remove操作

想要remove掉一个node,必须找到此node的前后两个nodes,并把他们这两个nodes连接起来。

  1. 找到head
  2. 沿着head一个个摸下去,且实时记录前方的node,摸到对应的item后链接该node的前后两个nodes
  3. 如果remove的那个node正好是head,则直接把head权给予接下去的那个node
class UnorderList:
def __init__(self):
self.head = None
def is_empty(self):
return self.head == None
def add(self, item):
temp = Node(item) #创建新结点
temp.set_next(self.head) #将新结点链接之前的list
self.head = temp #将新结点设为head
def size(self):
current = self.head
count = 0
while current != None:
count += count
current = current.get_next()
return count
def search(self, item):
current = self.head
found = False
while current != None and not found:
if current.get_data() == item:
found = True
else:
current = current.get_next()
return found
def remove(self, item):
current = self.head()
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous == None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())

如果你执行一个insert(0,item)操作在一个list上,时间复杂度为O(n)。 但是对于无序链表来说,执行一个相同的操作是需要O(1)的时间复杂度

有序链表

就是将无序链表中的每个node的data进行了变得有大小顺序了。Node的class没有变化。

class orderList:
def __init__(self): # 无变化
self.head = None
def is_empty(self): # 无变化
return self.head == None
def add(self, item): # 变化最大的在这里
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.get_data() > item:
stop = True
else:
previous = current
current = current.get_next()
temp = Node(item)
if previous == None:
temp.set_next(self.head)
self.head = temp
else:
temp.set_next(current)
previous.set_next(temp)
def size(self): # 无变化
current = self.head
count = 0
while current != None:
count += count
current = current.get_next()
return count
def search(self, item): # 其实基本没变化,stop可有可无
current = self.head
found = False
stop = False
while current != None and not found and not stop:
if current.get_data() == item:
found = True
else:
if current.get_data() > item:
stop = True
else:
current = current.get_next()
return found
def remove(self, item): # 无变化
current = self.head()
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous == None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())

Python数据结构应用3——链表的更多相关文章

  1. python数据结构与算法——链表

    具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...

  2. Python数据结构之单链表

    Python数据结构之单链表 单链表有后继结点,无前继结点. 以下实现: 创建单链表 打印单链表 获取单链表的长度 判断单链表是否为空 在单链表后插入数据 获取单链表指定位置的数据 获取单链表指定元素 ...

  3. python 数据结构之单链表的实现

    链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...

  4. python——数据结构之单链表的实现

    链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址 信息,所以用一个变量 ...

  5. Python数据结构——栈的链表实现

    自定义链表实现栈的数据结构,代码如下: class Stack: def __init__(self): self._first = None def push(self,item): self._f ...

  6. Python 数据结构与算法——链表

    #构造节点类 class Node(object): def __init__(self,data=None,_next=None): ''' self.data:为自定义的数据 self.next: ...

  7. python数据结构与算法

    最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...

  8. Python—数据结构——链表

    数据结构——链表 一.简介 链表是一种物理存储上非连续,数据元素的逻辑顺序通过链表中的指针链接次序,实现的一种线性存储结构.由一系列节点组成的元素集合.每个节点包含两部分,数据域item和指向下一个节 ...

  9. Python 数据结构和算法

    阅读目录 什么是算法 算法效率衡量 算法分析 常见时间复杂度 Python内置类型性能分析 数据结构 顺序表 链表 栈 队列 双端队列 排序与搜索 冒泡排序 选择排序 插入排序 希尔排序 快速排序 归 ...

随机推荐

  1. 使用nginx sticky实现基于cookie的负载均衡

    在多台后台服务器的环境下,我们为了确保一个客户只和一台服务器通信,我们势必使用长连接.使用什么方式来实现这种连接呢,常见的有使用nginx自带的ip_hash来做,我想这绝对不是一个好的办法,如果前端 ...

  2. 获取redis主从复制链SHELL脚本

    获取redis主从复制链SHELL脚本 vi redisnode.sh #!/bin/sh master_host=$ master_port=$ auth=$ #判断输入密码是否为空,为空则转化为' ...

  3. pg_dump命令帮助信息

    仅为参考查阅方便,完全命令行帮助信息,无阅读价值. pg_dump dumps a database as a text file or to other formats. Usage:  pg_du ...

  4. bootstrap响应式设计简单实践。

    首先需要熟悉Boostrap提供的响应式设施:http://getbootstrap.com/css/#responsive-utilities,BootStrap的响应式设施主要是利用媒体查询对元素 ...

  5. 浅析fork()和底层实现

    记得以前初次接触fork()函数的时候,一直被“printf”输出多少次的问题弄得比较晕乎.不过,“黄天不负留心人".哈~ 终于在学习进程和进程创建fork相关知识后,总算是大致摸清了其中的 ...

  6. printf("Hello 2018!");

    月考 has Boom! 要全心准备期末考试,到年前是不能再看Blog了 新年加油!!! 不要感冒 :joy:

  7. LCA最近公共祖先(倍增版)

    倍增版LCA lac即最近公共祖先,u和v最近公共祖先就是两节点公用的祖先中深度最大的 比如 其中 lca(1,2)=4, lca(2,3)=4, lca(3,5)=1, lca(2,5)=4; 如何 ...

  8. Linux——浅析信号处理

    信号及其处理 信号处理是Unix和LInux系统为了响应某些状况而产生的事件,通常内核产生信号,进程收到信号后采取相应的动作. 例如当我们想强制结束一个程序的时候,我们通常会给它发送一个信号,然后该进 ...

  9. leetCode刷题(找到最长的连续不重复的字符串长度)

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  10. 解决vue在ios或android中用webview打开H5链接时#号后面的参数被忽略问题angular同样适用

    在ios或android如果直接用webview在打开H5链接例如: 打开:http://localhost:8080/#/answer?id=1509335039582001 会变成 http:// ...