# -*- 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. Angular2.0的学习(四)

    第四节课:数据绑定.响应式编程和管道 1.数据绑定(插值表达式.事件绑定.属性绑定.双向绑定) 2.响应式编程 3.管道

  2. Python+Selenium----使用HTMLTestRunner.py生成自动化测试报告2(使用PyCharm )

    1.说明 在我前一篇文件(Python+Selenium----使用HTMLTestRunner.py生成自动化测试报告1(使用IDLE ))中简单的写明了,如何生产测试报告,但是使用IDLE很麻烦, ...

  3. Ubuntu批量修改文件后缀

    rename 's/\.JPG/.jpg/' *.JPG 把JPG后缀改为jpg 参考url====http://blog.csdn.net/whuslei/article/details/67249 ...

  4. Unity Destroy和DestroyImmediate

    Destroy(Object obj, float t = 0.0F); 删除一个游戏对象,组件或者资源. 物体obj现在被销毁或在指定了t时间过后销毁.如果obj是组件,它将从GameObject销 ...

  5. linux上的shutdown命令

    定时关机:shutdown -r now 立刻重新开机 shutdown -h now 立刻关机 shutdown -k now 'Hey! Go away! now....' 发出警告讯息, 但没有 ...

  6. Angular8稳定版修改概述

    在今天早些时候Angular团队发布了8.0.0稳定版.其实早在NgConf 2019大会上,演讲者就已经提及了从工具到差分加载的许多内容以及更多令人敬畏的功能.下面是我对8.0.0一些新功能的简单介 ...

  7. JQuery使用正则表达式验证手机号,邮箱,身份证(含有港澳台),网址

    自己对正则验证也没系统用过,这次自己做个demo,一下子把这些全都用上了,下次有需要直接来拿了. 以下代码是在页面使用JQuery进行验证的,也有在后台进行验证的,可以试试,都一样的原理. 直接上代码 ...

  8. 配置百度云盘python客户端bypy上传备份文件

    要求:安装python2.7,安装git 1.git clone https://github.com/houtianze/bypy.git 2.cd bypy 3.sudo python setup ...

  9. angular 学习笔记(3) ng-repeat遍历json并且给样式

    视图: <div ng-app="myapp" ng-controller="myctrl"> <ul> <li ng-repea ...

  10. 编译出freeswitch的java调用的 jar和so

    假设freeswitch 源码路径为 /usr/local/src/freeswitch 1. cd /usr/local/src/freeswitch(源代码的根目录) 执行./configure, ...