# -*- 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. Xml2Object

    <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream ...

  2. Python+Selenium----处理登录图片验证码

    1.说明 在做自动化测试的时候,经常会遇到登录,其中比较麻烦的就是验证码的处理,现在比较常用的图形验证码,每次刷新,得到的验证码不一致,所以,一般来说,获取验证码图片有两种方式: (1)拿到验证码的图 ...

  3. vue——做了一个幼稚的小页面

    我的小花花没有转起来,不开心  ̄へ ̄

  4. 020 Valid Parentheses 有效的括号

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效.括号必须以正确的顺序关闭,"()" 和 "()[]{}" 是有效的 ...

  5. .net 记录

    Stack Overflow 2016最新架构探秘 http://www.infoq.com/cn/news/2016/03/Stack-Overflow-architecture-insi#rd N ...

  6. Python 为threading.Thread添加 terminate

    import threading import inspect import ctypes def _async_raise(tid, exc_type): """rai ...

  7. Docker | 第七章:Docker Compose服务编排介绍及使用

    前言 前面章节,我们学习了如何构建自己的镜像文件,如何保存自己的镜像文件.大多都是一个镜像启动.当一个系统需要多个子系统进行配合时,若每个子系统也就是镜像需要一个个手动启动和停止的话,那估计实施人员也 ...

  8. 禁止Asp.Net WebService 的Test页面功能

    只需要Web.Config里面添加: <system.web> <webServices> <protocols> <remove name="Ht ...

  9. mysql数据库初步了解

    一丶数据库服务器丶数据管理系统丶数据库丶表与记录的关系 记录:1 xxxx 3245646546(多个字段的信息组成一条记录,即文件中的一行内容) 表: Student.school,class_li ...

  10. JavaScript初识(三)

    十三丶JS中的面向对象 创建对象的几种常用方式: 1.使用Object或对象字面量创建对象 2.工厂模式创建对象 3.构造函数模式创建对象 4.原型模式创建对象 下面我们详细看一下如何创建对象 1.使 ...