Python 数据结构与算法——链表
#构造节点类
class Node(object):
def __init__(self,data=None,_next=None):
'''
self.data:为自定义的数据
self.next:下一个节点的地址(这里为下一个数据建立了一个对象)
'''
self.data = data
self.next = _next def __repr__(self):
return str(self.data) #单链表
class MyChain(object):
def __init__(self):
'''初始化空链表'''
self.head=None
self.length=0 def __repr__(self):
""" 重构:对链表进行输出 """
temp = self.head
cstr = ""
for i in range(self.length-1):
cstr += str(temp.data)+' -> '
temp = temp.next
cstr += str(temp.data)
return cstr def isEmpty(self):
return (self.length==0) def append(self, mydata):
'''增加节点'''
item=None
if isinstance(mydata, Node):
item=mydata
else:
item=Node(mydata) if not self.head:
self.head=item
self.length +=1
else:
node=self.head
while node.next:
node=node.next
node.next=item
self.length += 1 def delete(self,index):
if self.isEmpty():
print("this chain table is empty")
return if index<0 or self.length<=index:
print("index is out of range")
return
if index==0:
self.head=self.head.next
self.length -=1
return
#prev为保存前导节点
#node为保存当前节点
#当j与index相等时就
#相当于找到要删除的节点
j=0
node=self.head
prev=self.head
while node.next and j < index:
prev = node
node = node.next
j += 1 if j == index:
prev.next = node.next
self.length -= 1 def update(self, index, data):
'''修改节点'''
if self.isEmpty() or index < 0 or index >= self.length:
print('error: out of index')
return
j = 0
node = self.head
while node.next and j < index:
node = node.next
j += 1 if j == index:
node.data = data def getItem(self, index):
'''查找节点'''
if self.isEmpty() or index < 0 or index >= self.length:
print("error: out of index")
return
j = 0
node = self.head
while node.next and j < index:
node = node.next
j += 1 return node.data def getIndex(self, data):
j = 0
if self.isEmpty():
print("this chain table is empty")
return
node = self.head
while node:
if node.data == data:
print("index is %s"%str(j))
return j
node = node.next
j += 1 if j == self.length:
print ("%s not found" % str(data))
return def insert(self, index, mydata):
if self.isEmpty():
print ("this chain tabale is empty")
return if index < 0 or index >= self.length:
print ("error: out of index")
return item = None
if isinstance(mydata, Node):
item = mydata
else:
item = Node(mydata) if index == 0:
item.next = self.head
self.head = item
self.length += 1
return j = 0
node = self.head
prev = self.head
while node.next and j < index:
prev = node
node = node.next
j += 1 if j == index:
item.next = node
prev.next = item
self.length += 1 def clear(self):
self.head = None
self.length = 0 if __name__=="__main__":
L = [1,2,3,4,5,6,7,8,9,0]
chain = MyChain()
for i in L:
n = Node(i)
chain.append(n)
chain.getIndex(2)
chain.insert(0, 0)
chain.getItem(9)
chain.update(3, 77)
print(chain)
Python 数据结构与算法——链表的更多相关文章
- python数据结构与算法——链表
具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...
- python数据结构与算法
最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...
- Python数据结构与算法--List和Dictionaries
Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...
- Python数据结构与算法--算法分析
在计算机科学中,算法分析(Analysis of algorithm)是分析执行一个给定算法需要消耗的计算资源数量(例如计算时间,存储器使用等)的过程.算法的效率或复杂度在理论上表示为一个函数.其定义 ...
- Python数据结构之单链表
Python数据结构之单链表 单链表有后继结点,无前继结点. 以下实现: 创建单链表 打印单链表 获取单链表的长度 判断单链表是否为空 在单链表后插入数据 获取单链表指定位置的数据 获取单链表指定元素 ...
- JavaScript数据结构与算法-链表练习
链表的实现 一. 单向链表 // Node类 function Node (element) { this.element = element; this.next = null; } // Link ...
- Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例
本文实例讲述了Python数据结构与算法之图的最短路径(Dijkstra算法).分享给大家供大家参考,具体如下: # coding:utf-8 # Dijkstra算法--通过边实现松弛 # 指定一个 ...
- Python数据结构与算法之图的广度优先与深度优先搜索算法示例
本文实例讲述了Python数据结构与算法之图的广度优先与深度优先搜索算法.分享给大家供大家参考,具体如下: 根据维基百科的伪代码实现: 广度优先BFS: 使用队列,集合 标记初始结点已被发现,放入队列 ...
- Python 数据结构和算法
阅读目录 什么是算法 算法效率衡量 算法分析 常见时间复杂度 Python内置类型性能分析 数据结构 顺序表 链表 栈 队列 双端队列 排序与搜索 冒泡排序 选择排序 插入排序 希尔排序 快速排序 归 ...
随机推荐
- SpringMVC中ApplicationContext中的配置文件的问题(No bean named 'sessionFactory' is defined 已解决)
在一个SpringMVC项目中, 连着两天不管怎么搞都是一直在报错, 报的最多的就是一个 Servlet.service() for servlet [springDispatcherServlet] ...
- hbase概念
1. 概述(扯淡~) HBase是一帮家伙看了Google发布的一片名为“BigTable”的论文以后,犹如醍醐灌顶,进而“山寨”出来的一套系统. 由此可见: 1. 几乎所有的HBase中的理念,都可 ...
- 鼠标滑过图片添加边框图片无位移[xyytit]
实现下面的效果,鼠标滑过图片添加边框图片无位移——鼠标滑过,图片只是加了边框,不会晃动: 参考代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...
- Java计算图的匹配率
2016-07-02 大概意思就是这样了,代码里我貌似没有计算最后一步,但是原理都是一样的.....R1有5个点P1有四个点,他们共同的点是4个,那就是共同点4*4/(R1的5个点*P1的四个点就是0 ...
- [udemy]WebDevelopment_HTML5
Build Your First Website 装一个subline text HTML default rule tags with opening and closing <!DOCTY ...
- JsonConvert.SerializeObject 空值处理
var settings = new JsonSerializerSettings() { ContractResolver= new NullToEmptyStringResolver() }; v ...
- C语言跳表(skiplist)实现
一.简介 跳表(skiplist)是一个非常优秀的数据结构,实现简单,插入.删除.查找的复杂度均为O(logN).LevelDB的核心数据结构是用跳表实现的,redis的sorted set数据结构也 ...
- Alluxio/Tachyon如何发挥lineage的作用?
在Spark的RDD中引入过lineage这一概念.指的是RDD之间的依赖.而Alluxio则使用lineage来表示文件之间的依赖.在代码层面,指的是fileID之间的依赖. 代码中的注释指出: * ...
- 连接数据库-stone
# -*- coding:utf-8 -*- import pymysql class mysql: def __init__(self, host, port, dbuser, dbpwd, dbn ...
- win10如何安装和创建 证书
.下载winsdksetup.exe .在 MMC 管理单元中查看证书 打开一个命令提示符窗口. 类型mmc然后按 ENTER 键. 请注意,若要查看本地计算机存储中的证书,您必须具有管理员角色. 上 ...