python 实现无序列表
# -*- coding:utf-8 -*-
class Node:
def __init__(self, initdata):
self.data = initdata
self.next = None def getData(self):
return self.data def getNext(self):
return self.next def setData(self, newdata):
self.data = newdata def setNext(self, newnext):
self.next = newnext class UnorderedList:
def __init__(self):
self.head = None def isEmpty(self):
return self.head == None def add(self, item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()
return count def travel(self):
current = self.head
while current != None:
print current.getData()
current = current.getNext() def search(self, item):
current = self.head
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found def remove(self, item):
current = self.head
previous = None
found = False
while not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext()) def append(self, item):
temp = Node(item)
if self.isEmpty():
self.head = temp
else:
current = self.head
while current.getNext() != None:
current = current.getNext()
current.setNext(temp) def index(self, item):
current = self.head
count = 0
found = False
while current != None and not found:
count += 1
if current.getData() == item:
found = True
else:
current = current.getNext()
if found:
return count
else:
raise ValueError, '%s is not in this unorderedList' %item def insert(self, pos, item):
if pos <= 1:
self.add(item)
elif pos > self.size():
self.append(item)
else:
temp = Node(item)
count = 1
previous = None
current = self.head
while count < pos:
count += 1
previous = current
current = current.getNext()
previous.setNext(temp)
temp.setNext(current) if __name__ == '__main__':
t = UnorderedList()
for i in range(10):
t.append(i)
print t.size()
t.travel()
print t.search(5)
print t.index(3)
t.remove(8)
t.travel()
t.insert(2, 12)
t.travel()
python 实现无序列表的更多相关文章
- python实现无序列表:链表
介绍链表前我们先了解下什么是列表. 在对基本数据结构的讨论中,我们使用 Python 列表来实现所呈现的抽象数据类型.列表是一个强大但简单的收集机制,为程序员提供了各种各样的操作.然而,不是所有的编程 ...
- Python实践练习:在 Wiki 标记中添加无序列表
题目描述 项目:在 Wiki 标记中添加无序列表 在编辑一篇维基百科的文章时,你可以创建一个无序列表,即让每个列表项占据一行,并在前面放置一个星号.但是假设你有一个非常大的列表,希望添加前面的星号.你 ...
- Python中将字典转换为有序列表、无序列表的方法
说明:列表不可以转换为字典 1.转换后的列表为无序列表 a = {'a' : 1, 'b': 2, 'c' : 3} #字典中的key转换为列表 key_value = list(a.keys()) ...
- [转载]Python 元组、列表、字典、文件
python的元组.列表.字典数据类型是很python(there python is a adjective)的数据结构.这些结构都是经过足够优化后的,所以如果使用好的话,在某些area会有很大的益 ...
- 在 Wiki 标记中添加无序列表
项目:在 Wiki 标记中添加无序列表在编辑一篇维基百科的文章时,你可以创建一个无序列表,即让每个列表项占据一行,并在前面放置一个星号.但是假设你有一个非常大的列表,希望添加前面的星号.你可以在每一行 ...
- Python基础数据类型-列表(list)和元组(tuple)和集合(set)
Python基础数据类型-列表(list)和元组(tuple)和集合(set) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的 ...
- Python基本数据类型--列表、元组、字典、集合
一.Python基本数据类型--列表(List) 1.定义:[ ]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素. 2.列表的创建: # 方式一 list1 = ['name','ag ...
- python 迭代 及列表生成式
什么是迭代 在Python中,如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们成为迭代(Iteration). 在Python中,迭代是通过 for ...
- Python 学习笔记(1)Python容器:列表、元组、字典与集合
Python容器:列表.元组.字典与集合 列表: 1.列表 的创建 使用[ ] 或者 list()创建列表:empty_list = [ ] 或者 empty_list= list() 使用list( ...
随机推荐
- VSCode makedown增强插件
Markdown Preview Enhanced https://shd101wyy.github.io/markdown-preview-enhanced/#/zh-cn/
- HDU 5917 Instability ramsey定理
http://acm.hdu.edu.cn/showproblem.php?pid=5917 即世界上任意6个人中,总有3个人相互认识,或互相皆不认识. 所以子集 >= 6的一定是合法的. 然后 ...
- 车牌号校验js
var regExp = /(^[\u4E00-\u9FA5]{1}[A-Z0-9]{6}$)|(^[A-Z]{2}[A-Z0-9]{2}[A-Z0-9\u4E00-\u9FA5]{1}[A-Z0-9 ...
- 3 - Selenium元素定位和操作
3.1定位 <button id="gbqfba" aria-label="Google Search" name="btnK" cl ...
- 性能测试学习第十天_controller
集合点设置 controller虚拟多个用户执行脚本启动步骤不一定同步,集合点在脚本的某处设置一个标记,当有虚拟用户运行到这个标记的时候,停下等待所有用户都达到这个标记,再一同进行下面的步骤.这样可以 ...
- selenium常用方法,简版介绍
WebElement 接口共计16个------------接口 代表一个HTML元素.通常,所有与页面交互有关的有趣操作都将通过此界面执行. void clear() void click() We ...
- drools的error:Jboss rule 6.4.0 Cannot find a default StatelessKieSession
drools的kmodule.xml文件,如果是默认加载必须放在META-INF的文件夹下,如下图 官网大佬原话 Have you defined kieSession and KieBase nam ...
- ASP.NET MVC缓存
根据缓存的位置不同,可以区分为: ①客户端缓存(缓存在用户的客户端,例如浏览器中) ②服务器缓存(缓存在服务器中,可以缓存在内存中,也可以缓存在文件里,并且还可以进一步地区分为本地缓存和分布式缓存两种 ...
- lazyload的使用心得
1 2 3 4 5 6 7 8 9 10 11 12 13 14 $("img.lazy").lazyload({ placeholder : "img/grey.g ...
- 编程中的多字节和Unicode
在编译许多程序的时候,我们常常会出现诸如指针转换错误或者const char[] 不能转换成XX的错误,这时很可能就是项目编码的问题了,如果您使用的是VS编程环境,那么打开工程属性,里面就有个选项是给 ...