#构造节点类
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. 第五章 二叉树(a)树

  2. 运行 命令框不记录打过的命令,重启后CMD里面是空的.上次打过的命令消失了.

    问题: 常要用到PING命令.在cmd中输入ping 202.103.44.150 /t (这是当地的电信DNS) 用这个查看网络是不是正常.正常情况下次点开始运行的时候,运行命令框中应该 会有上次打 ...

  3. Birthday(费用流)

    Birthday https://www.nowcoder.com/acm/contest/206/A 题目描述 恬恬的生日临近了.宇扬给她准备了一个蛋糕. 正如往常一样,宇扬在蛋糕上插了n支蜡烛,并 ...

  4. 蚁群算法(Java)tsp问题

      1.理论概述 1.1.TSP问题 旅行商问题,即TSP问题(旅行推销员问题.货郎担问题),是数学领域中著名问题之一.假设有一个旅行商人要拜访n个城市,他必须选择所要走的路径,路径的限制是每个城市只 ...

  5. centos 6.5 ftp服务配置及客户端使用

    一.ftp服务简介 FTP是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”.用于Internet上的控制文件的双向传输.同时,它也是一个应用程序(Ap ...

  6. dede 复制文章,远程图片无法本地化

    解决方法: 1.找到dede的后台目录,在后台目录下的inc下找到inc_archives_functions.php 2.搜索GetCurContent函数,找到如下这段代码: preg_match ...

  7. css浮动的元素居中

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. jQuery 操作 html5 data-* 属性

    Html 部分: <a class="nav-item" href="javascript: void(0)" data-id="{{$item ...

  9. smrtlink

    SMRT Link is the web-based end-to-end workflow manager for the Sequel™ System. It includes software ...

  10. 纯css手写圆角气泡对话框 微信小程序和web都适用

    嗯……我们设计师强烈要求一定要圆角!圆角的气泡对话框,不要那种尖角的.这其中还遇上了个尴尬的问题,z-index不生效 无非就是两种方法,一种是使用图片再定位拼接起来使用,太简单了具体就不详细的说了. ...