#构造节点类
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. 安装Ubuntu后一些准备

    一些基础 安装的时候,先不选镜像就可以避开简易安装. 更改root密码:sudo passwd root 更改源,更新,不行就打断在更新 安装vim 改为unity模式,安装VMware Tools, ...

  2. Java数据类型与MySql数据类型对照表

    这篇文章主要介绍了Java数据类型与MySql数据类型对照表,以表格形式分析了java与mysql对应数据类型,并简单讲述了数据类型的选择与使用方法,需要的朋友可以参考下 本文讲述了Java数据类型与 ...

  3. 把html标签转换为实体 dhtmlspecialchars

    把html标签转换为实体/*可以处理数组中的代码,他们的作用是可以把一个数组或字符串中的字符转化为html实体,可以防止页面的跨站问题,那么我们看到他的转换就是将'&','"','& ...

  4. sql优化常用命令总结

    1.显示执行计划的详细步骤 SET SHOWPLAN_ALL ON; SET SHOWPLAN_ALL OFF; 2. 显示执行语句的IO成本,时间成本 SET STATISTICS IO ON SE ...

  5. Ubuntu下的网络服务

    一.Telnet Telnet是teletype network的缩写,表示远程登录协议和方式,分为Telnet客户端和Telnet服务器程序. Telnet服务虽然也属于客户机/服务器模型的服务,但 ...

  6. LUOGU P4408 [NOI2003]逃学的小孩(树的直径)

    题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:“喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?”一听说要考试,Chris的父母就心急如焚,他们决定在尽 ...

  7. c++11 随机代码记录

    // RadomTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #includ ...

  8. 20155210 2016-2017-2 《Java程序设计》第7周学习总结

    20155210 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量: GMT(Greenwich Mean Time)时间:现在不是标准时间 世界时 ...

  9. 2018.08.27 rollcall(非旋treap)

    描述 初始有一个空集,依次插入N个数Ai.有M次询问Bj,表示询问第Bj个数加入集合后的排名为j的数是多少 输入 第一行是两个整数N,M 接下来一行有N个整数,Ai 接下来一行有M个整数Bj,保证数据 ...

  10. yii框架场景的用法

    1.在 model 里面定义一下场景 类名必须是 scenarios() public function scenarios() { return [ 'create' => ['title', ...