#构造节点类
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 数据结构与算法——链表的更多相关文章

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

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

  2. python数据结构与算法

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

  3. Python数据结构与算法--List和Dictionaries

    Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...

  4. Python数据结构与算法--算法分析

    在计算机科学中,算法分析(Analysis of algorithm)是分析执行一个给定算法需要消耗的计算资源数量(例如计算时间,存储器使用等)的过程.算法的效率或复杂度在理论上表示为一个函数.其定义 ...

  5. Python数据结构之单链表

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

  6. JavaScript数据结构与算法-链表练习

    链表的实现 一. 单向链表 // Node类 function Node (element) { this.element = element; this.next = null; } // Link ...

  7. Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例

    本文实例讲述了Python数据结构与算法之图的最短路径(Dijkstra算法).分享给大家供大家参考,具体如下: # coding:utf-8 # Dijkstra算法--通过边实现松弛 # 指定一个 ...

  8. Python数据结构与算法之图的广度优先与深度优先搜索算法示例

    本文实例讲述了Python数据结构与算法之图的广度优先与深度优先搜索算法.分享给大家供大家参考,具体如下: 根据维基百科的伪代码实现: 广度优先BFS: 使用队列,集合 标记初始结点已被发现,放入队列 ...

  9. Python 数据结构和算法

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

随机推荐

  1. Connections in Galaxy War(逆向并查集)

    Connections in Galaxy War http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563 Time Limit ...

  2. 全国省市区数据库SQL(有可能不是最新的)

    百度云下载地址:https://pan.baidu.com/s/1lStN7tYpwOtpC-r3G2X2sw

  3. 安装tftp服务器进行文件传输

    1. 安装: sudo apt-get install tftp-hpa tftpd-hpa ps: tftpd是服务器,tftp是客户端,客户端能发送和获取,服务器不能动. 2. 配置文件: sud ...

  4. 修改UITextView光标高度

    自定义UITextView文字字体时,经常出现光标与字体的高度不匹配,可以通过下面代码修改默认的光标高度, //创建子类重写UITextView方法 - (CGRect)caretRectForPos ...

  5. 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...

    链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...

  6. SourceTree下载 及使用

    SourceTree 代码库管理工具 https://www.cnblogs.com/QianChia/p/8531725.html#_label0 SourceTree的基本使用 https://w ...

  7. ubuntu14.04安装opengl

    OpenGL 是一套由SGI公司发展出来的绘图函式库,它是一组 C 语言的函式,用于 2D 与 3D 图形应用程式的开发上. 不可或缺的就是编译器与基本的函式库,如果系统没有安装的话,依照下面的方式安 ...

  8. 接触mybatis使用

    1.mybatis mybatis是一个自定义sql.存储过程和高级映射的持久层框架,是Apache下的顶级项目. mybatis可以让程序员将主要精力放在sql上,通过mybatis提供的映射方式. ...

  9. 记录java版本不兼容的坑,(kafka运行报错)

    启动kafka报错 错误原因是: 由较高版本的jdk编译的java class文件 试图在较低版本的jvm上运行的报错 解决办法是: 查看java版本 C:\Users\Administrator&g ...

  10. 运行代码后出现Process finished with exit code 0是为什么?

    Process finished with exit code 0 意味着你的程序正常执行完毕并退出. 可以科普一下exit code,在大部分编程语言中都适用: exit code 0 表示程序执行 ...