Python数据结构应用3——链表
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
结点的next
为None
。
无序链表
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语言写还是有差别的。
整个过程如图,增加的结点位于链表的头部,也就是说,向前增加新结点:
- 创建新结点
- 将新结点的next指向旧链表的head
- 将新链表的head设为新结点
‘遍历’操作(traverse)
get_next()方法是该操作中主要用到的函数。
size, search, remove 都是基于‘遍历’操作
size操作
- 找到head
- 沿着head一个个摸下去,摸一个count+1
search操作
- 找到head
- 沿着head一个个摸下去,摸到item返回True,摸不到继续往下摸
remove操作
想要remove掉一个node,必须找到此node的前后两个nodes,并把他们这两个nodes连接起来。
- 找到head
- 沿着head一个个摸下去,且实时记录前方的node,摸到对应的item后链接该node的前后两个nodes
- 如果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——链表的更多相关文章
- python数据结构与算法——链表
具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...
- Python数据结构之单链表
Python数据结构之单链表 单链表有后继结点,无前继结点. 以下实现: 创建单链表 打印单链表 获取单链表的长度 判断单链表是否为空 在单链表后插入数据 获取单链表指定位置的数据 获取单链表指定元素 ...
- python 数据结构之单链表的实现
链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...
- python——数据结构之单链表的实现
链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址 信息,所以用一个变量 ...
- Python数据结构——栈的链表实现
自定义链表实现栈的数据结构,代码如下: class Stack: def __init__(self): self._first = None def push(self,item): self._f ...
- Python 数据结构与算法——链表
#构造节点类 class Node(object): def __init__(self,data=None,_next=None): ''' self.data:为自定义的数据 self.next: ...
- python数据结构与算法
最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...
- Python—数据结构——链表
数据结构——链表 一.简介 链表是一种物理存储上非连续,数据元素的逻辑顺序通过链表中的指针链接次序,实现的一种线性存储结构.由一系列节点组成的元素集合.每个节点包含两部分,数据域item和指向下一个节 ...
- Python 数据结构和算法
阅读目录 什么是算法 算法效率衡量 算法分析 常见时间复杂度 Python内置类型性能分析 数据结构 顺序表 链表 栈 队列 双端队列 排序与搜索 冒泡排序 选择排序 插入排序 希尔排序 快速排序 归 ...
随机推荐
- Day15 jss整体结构梳理
JS DOM--- 两个步骤: 1 查找标签 (1)直接查找 document.getElementById("idname") // dom对象 document.getElem ...
- 基于libevent的tcp拆包分包库
TCP/IP协议虽然方便,但是由于是基于流的传输(UDP是基于数据报的传输),无论什么项目,总少不了解决拆包分包问题. 以前的项目总是每个程序员自己写一套拆包分包逻辑,实现的方法与稳定性都不太一致.终 ...
- spring cloud 入门系列五:使用Feign 实现声明式服务调用
一.Spring Cloud Feign概念引入通过前面的随笔,我们了解如何通过Spring Cloud ribbon进行负责均衡,如何通过Spring Cloud Hystrix进行服务断路保护,两 ...
- Spring框架碰壁日常更新
关于Spring环境搭建遇到的问题:
- 爬虫之proxy(代理)
proxy简介 proxy即为代理,我们爬虫的时候肯定会有频繁访问某一网站的情况,这个时候有些服务器会识别到我们是非正常访问,就会把我们的IP禁掉,这个时候就需要用代理了. 就好比现实生活中,我需要向 ...
- .net捕捉全局未处理异常的3种方式
前言: 我们在实际项目开发中,经常会遇到一些不可预见的异常产生,有的异常在程序运行时就对其进行处理(try)但是,有的程序不需要每一个地方都用try进行处理,那么针对这种情况,可以参照下面的方式,实现 ...
- leetCode刷题(将字符串转成W形状的字符串再以Z形字符串输出)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- java面试总结
一.java的集合框架 HashMap.HashTable.CurrentHashMap的底层数据结构与区别? CurrentHashMap与HashTable是如何保证线程安全的? ArrayLis ...
- cocos2d-x工作小记
1.当一个layer跳到下一个layer时,需要传递数据,可以默认定义一个setUserData()方法. 2.cocos2d-x不使用传统的值类型,所有的对象都创建在堆上,然后通过指针引用. 3.传 ...
- 破解linux虚拟机的密码
虚拟机破解秘密码步骤: 虚拟机(server)的登录通常需要一个本地用户,而本地用户密码假如不知道或者是已经忘记了,也是有办法进入的,在Linux系统内就有可以提供这种可以进入的方案 ...