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内置类型性能分析 数据结构 顺序表 链表 栈 队列 双端队列 排序与搜索 冒泡排序 选择排序 插入排序 希尔排序 快速排序 归 ...
随机推荐
- hdoj1176 免费馅饼(dp 数塔)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1176 思路: 这道题不复杂,很明显是个dp题,数据比较大,搜索应该会超时,想到如何初始化,注意细节就差 ...
- LuoguP1032 字符变换(BFS)
题目链接为:https://www.luogu.org/problemnew/show/P1032 思路:看到数据比较小,而且最多有6个规则,就可以用搜索去做了,我用的BFS,大体思路如下: 定义结构 ...
- 88. Merge Sorted Array 后插
合并两个排序的整数数组A和B变成一个新的数组.给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B ...
- 9-sort使用时的错误
/* 矩形嵌套 题目内容: 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形 ...
- Phalcon Framework的MVC结构及启动流程分析
目前的项目中选择了Phalcon Framework作为未来一段时间的核心框架.技术选型的原因会单开一篇Blog另说,本次优先对Phalcon的MVC架构与启动流程进行分析说明,如有遗漏还望指出. P ...
- Alluxio/Tachyon如何发挥lineage的作用?
在Spark的RDD中引入过lineage这一概念.指的是RDD之间的依赖.而Alluxio则使用lineage来表示文件之间的依赖.在代码层面,指的是fileID之间的依赖. 代码中的注释指出: * ...
- 一款APP的交互文档从撰写到交付
我第一份工作的设计总监是前百度设计师,34岁,一线设计12年:今年聊天说转了产品总监,如今39岁还活跃在行业中…… 我第二份工作的部门总监是前腾讯工程师,38岁,一线开发14年:2年前在Q群里跟我们说 ...
- Zookeeper 系列(五)Curator API
Zookeeper 系列(五)Curator API 一.Curator 使用 Curator 框架中使用链式编程风格,易读性更强,使用工程方法创建连接对象使用. (1) CuratorFramewo ...
- pyspider示例代码六:传递参数
传递参数 示例一 #!/usr/bin/env python # -*- encoding: utf- -*- # vim: ts= sts= ff=unix fenc=utf8: # Created ...
- vue组件介绍
https://www.cnblogs.com/Leo_wl/p/5863185.html vue.js说说组件 什么是组件:组件是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可重 ...