# -*- 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 实现无序列表的更多相关文章

  1. python实现无序列表:链表

    介绍链表前我们先了解下什么是列表. 在对基本数据结构的讨论中,我们使用 Python 列表来实现所呈现的抽象数据类型.列表是一个强大但简单的收集机制,为程序员提供了各种各样的操作.然而,不是所有的编程 ...

  2. Python实践练习:在 Wiki 标记中添加无序列表

    题目描述 项目:在 Wiki 标记中添加无序列表 在编辑一篇维基百科的文章时,你可以创建一个无序列表,即让每个列表项占据一行,并在前面放置一个星号.但是假设你有一个非常大的列表,希望添加前面的星号.你 ...

  3. Python中将字典转换为有序列表、无序列表的方法

    说明:列表不可以转换为字典 1.转换后的列表为无序列表 a = {'a' : 1, 'b': 2, 'c' : 3} #字典中的key转换为列表 key_value = list(a.keys()) ...

  4. [转载]Python 元组、列表、字典、文件

    python的元组.列表.字典数据类型是很python(there python is a adjective)的数据结构.这些结构都是经过足够优化后的,所以如果使用好的话,在某些area会有很大的益 ...

  5. 在 Wiki 标记中添加无序列表

    项目:在 Wiki 标记中添加无序列表在编辑一篇维基百科的文章时,你可以创建一个无序列表,即让每个列表项占据一行,并在前面放置一个星号.但是假设你有一个非常大的列表,希望添加前面的星号.你可以在每一行 ...

  6. Python基础数据类型-列表(list)和元组(tuple)和集合(set)

    Python基础数据类型-列表(list)和元组(tuple)和集合(set) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的 ...

  7. Python基本数据类型--列表、元组、字典、集合

    一.Python基本数据类型--列表(List) 1.定义:[ ]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素. 2.列表的创建: # 方式一 list1 = ['name','ag ...

  8. python 迭代 及列表生成式

    什么是迭代 在Python中,如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们成为迭代(Iteration). 在Python中,迭代是通过 for ...

  9. Python 学习笔记(1)Python容器:列表、元组、字典与集合

    Python容器:列表.元组.字典与集合 列表: 1.列表 的创建 使用[ ] 或者 list()创建列表:empty_list = [ ] 或者 empty_list= list() 使用list( ...

随机推荐

  1. Helvetic Coding Contest 2016 online mirror D1

    Description "The zombies are lurking outside. Waiting. Moaning. And when they come..." &qu ...

  2. Linux之dstat命令

    dstat命令是一个用来替换vmstat.iostat.netstat.nfsstat和ifstat这些命令的工具,是一个全能系统信息统计工具.与sysstat相比,dstat拥有一个彩色的界面,在手 ...

  3. 练习二十二:python兵乓求比赛顺序练习,关于连个兵乓球队进行比赛

    已知有两支兵乓球队进行比赛,每队各出3人: 甲队有a,b,c三人,乙队有x,y,z三人,已抽签决定比赛名单 问题:有人向队员打听比赛名单.a说他不和X比,c说他不和x,z比,程序找出比赛对手 方法一: ...

  4. IIS断开连接之后internet信息服务里面不显示本地计算机的解决方法

    今天我断开了IIS的本地计算机连接之后,出现了无法连接的情况.具体如图: 解决方法: 右击->所有服务->重新启动iis即可.

  5. CF1136D Nastya Is Buying Lunch

    思路: 1. 最终答案不超过能与Nastya“直接交换”的人数. 2. 对于排在j前面的i,如果i和i-j之间(包括j)的每个人都能“直接交换”,j才能前进一步. 实现: #include <b ...

  6. This is your path and you will pursue it with excellence.

    This is your path and you will pursue it with excellence.自己选的路就要走出精彩.

  7. 【Shell脚本学习25】Shell文件包含

    像其他语言一样,Shell 也可以包含外部脚本,将外部脚本的内容合并到当前脚本. Shell 中包含脚本可以使用: . filename 或 source filename 两种方式的效果相同,简单起 ...

  8. 【java】使用URL和CookieManager爬取页面的验证码和cookie并保存

    使用java的net包和io包下的几个工具爬取页面的验证码图片并保存到本地. 然后可以把获取的cookie保存下来,做进一步处理.比如通过识别验证码,进一步使用验证码和用户名,密码,保存下来的cook ...

  9. Hyper-V 2016 配置管理系列(准备篇)

    2.1 推荐软硬件配置 2.2 Hyper主机前提准备 前提条件: 具有二级地址转换(SLAT)的64位处理器.要安装Hyper-V虚拟化组件(如Windows管理程序),处理器必须具有SLAT 足够 ...

  10. C语言标准库之setjmp

    协程的介绍 协程(coroutine),意思就是“协作的例程”(co-operative routines),最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程 ...