Python单链表实现
class Node():
def __init__(self,InitDate):
self.Date=InitDate
self.next=None
def setNext(self,newnext):
self.next=newnext
def setDate(self,newDate):
self.Date=newDate
def getNext(self):
return self.next
def getDate(self):
return self.Date
class LinkedList():
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+=1
current=current.getNext()
return count
def show(self):
current=self.head
while(current!=None):
print current.getDate(),
current=current.getNext()
print " "
def search(self,item):
current=self.head
found=False
while not found and (current != None):
if current.getDate()==item:
found=True
else:
current=current.getNext()
print found
def remove(self,item):
previous=None
current=self.head
found=False
while not found and (current != None):
if current.getDate()==item:
found=True
else:
previous=current
current=current.getNext()
if found==False:
print "not {0}".format(item)
elif current==self.head:
self.head=current.getNext()
else:
previous.setNext(current.getNext())
def insert(self,index,item):
previous=None
current=self.head
count=0
temp=Node(item)
if index>self.size():
print "out index"
elif index==0:
temp.setNext(current)
self.head=temp
else:
while index:
index-=1
previous=current
current=current.getNext()
previous.setNext(temp)
temp.setNext(current)
if __name__=="__main__":
alist=LinkedList()
for i in range(10):
alist.add(i)
alist.show()
print alist.size()
alist.remove(5)
alist.show()
alist.insert(7,110)
alist.show()
alist.search(110)
输出:
9 8 7 6 5 4 3 2 1 0
10
9 8 7 6 4 3 2 1 0
9 8 7 6 4 3 2 110 1 0
True
Python单链表实现的更多相关文章
- 用最简单的方式学Python单链表
Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式 ...
- python单链表
#!/usr/bin/env python3 # -*- coding:utf-8 -*- class LNode: """ 结点类 """ ...
- python单链表的基本操作思路
单链表: 1.定义链表 class ListNode: # 定义节点 def __init__(self, x): self.val = x # 节点当前值 self.next = None # 指向 ...
- 数据结构:单链表结构字符串(python版)添加了三个新功能
#!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...
- 数据结构:单链表结构字符串(python版)改进
此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...
- 数据结构:单链表结构字符串(python版)
#!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...
- Python 之简易单链表
单链表的基本要素有 2 个,数据项和连接项.这两项在 Python 中可以通过对象及其属性来实现. class Node: def __init__ (self, data): self.data = ...
- python 数据结构之单链表的实现
链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...
- python实现数据结构单链表
#python实现数据结构单链表 # -*- coding: utf-8 -*- class Node(object): """节点""" ...
随机推荐
- OpenGL阴影,Shadow Volumes(附源程序,使用 VCGlib )
实验平台:Win7,VS2010 先上结果截图: 本文是我前一篇博客:OpenGL阴影,Shadow Mapping(附源程序)的下篇,描述两个最常用的阴影技术中的第二个,Shadow Volu ...
- 2016年JS面试题目汇总
1.怎样添加.移除.移动.复制.创建和查找节点? //1)创建新节点 createDocumentFragment() //创建一个DOM片段 createElement() //创建一个具体的元素 ...
- Python 中的map和reduce学习笔记
map和reduce都是Python中的内置函数 map函数接受两个参数,第一个参数是函数,第二个参数是列表,将函数依次作用于列表中的元素,并返回一个元素 reduce同样以函数和列表作为参数,区别在 ...
- java虚拟机之垃圾收集器
serial收集器: 最基本的,是一个单线程收集器,只会使用一个CPU或者一条收集线程去完成垃圾收集, 更重要的是 在进行垃圾收集时,其他任务线程必须停止,serial收集器任然是client模式下的 ...
- 十分钟让你的javascript登峰造极
javascipt被称作前端的灵魂,没法灵活运用它,你的前端就只是一具行死走肉.大多初学者能顺利度过div+css,然后倒在了js怀抱,即时跨过了这一关,也只是会用,其底层原理一概不知.小编这就带大家 ...
- Xcode8 上传完.ipa包 官网超过2天还没反应
出现这个问题一般邮件有提示,我这里说一下,我之前都上传没有问题,就更新完Xcode8,就不行. 这个问题其实是因为权限没有写完 这样就可以了.
- 动手动脑:Finally
Ø当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机. Ø请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结. Ø特别注意: Ø当有多 ...
- Dubbo框架
1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需 ...
- 数位DP之奥义
恩是的没错数位DP的奥义就是一个简练的dfs模板 int dfs(int position, int condition, bool boundary) { ) return (condition ? ...
- div模态层示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...