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单链表实现的更多相关文章

  1. 用最简单的方式学Python单链表

    Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式 ...

  2. python单链表

    #!/usr/bin/env python3 # -*- coding:utf-8 -*- class LNode: """ 结点类 """ ...

  3. python单链表的基本操作思路

    单链表: 1.定义链表 class ListNode: # 定义节点 def __init__(self, x): self.val = x # 节点当前值 self.next = None # 指向 ...

  4. 数据结构:单链表结构字符串(python版)添加了三个新功能

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

  5. 数据结构:单链表结构字符串(python版)改进

    此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...

  6. 数据结构:单链表结构字符串(python版)

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

  7. Python 之简易单链表

    单链表的基本要素有 2 个,数据项和连接项.这两项在 Python 中可以通过对象及其属性来实现. class Node: def __init__ (self, data): self.data = ...

  8. python 数据结构之单链表的实现

    链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...

  9. python实现数据结构单链表

    #python实现数据结构单链表 # -*- coding: utf-8 -*- class Node(object): """节点""" ...

随机推荐

  1. track by

    ng-repeat指令中使用track by子语句解决重复数据遍历的错误 <li ng-repat="x in [2, 2]" ng-bind="x"&g ...

  2. cs231n笔记:最优化

    本节是cs231学习笔记:最优化,并介绍了梯度下降方法,然后应用到逻辑回归中 引言 在上一节线性分类器中提到,分类方法主要有两部分组成:1.基于参数的评分函数.能够将样本映射到类别的分值.2.损失函数 ...

  3. C语言的作用域规则

    作用域规则 程序中名字的作用域,通俗的讲,就是这个名字在程序中的使用范围.在C语言中,作用域的规则相对比较简单,主要分为 全局作用域 和 局部作用域 两种. 一个变量如果定义在函数中,那么这个变量的作 ...

  4. 在Visual Lisp中处理自动化错误

    Handling Automation errors in Visual LISP 翻译自原文Kean's blog:http://through-the-interface.typepad.com/ ...

  5. event.stopPropagation()与event.preventDefault()

    <div id='div0'> <div id='div1'> <a href="#" id='div2'>2222</a> < ...

  6. textarea关于空格和换行那点事

    textarea中空格连续输入多个的情况下,数据回显的时候页面只是显示一个:换行同样有问题,在textarea中有换行,在页面上却没有,今天终于看到个写的比较具体的文章,拿过来收藏下. 地址链接: h ...

  7. 评价qq拼音输入法

    我目前正在使用qq拼音输入法,从人机交互设计方面,我对qq输入法从用户界面.记住用户选择.短期刺激和长期使用的好处坏处.不让用户犯简单错误这四个方面进行了评价. 1.从用户界面方面: qq输入法用户界 ...

  8. CE 进程间通信

    WINCE下进程间通信常用的方式有:剪贴板(Clipboard),网络套接字(Socket),WM_COPYDATA消息,共享内存,管道(消息队列),注册表等 剪贴板 //////////////// ...

  9. ie6对hover兼容性问题的解决:

    ie6对hover兼容性问题的解决: 1,在body里添加以下样式: behavior:url(../scripts/csshover.htc); csshover.htc可直接在网上下载 2,js解 ...

  10. 【jq】c#零基础学习之路(3)继承和虚方法

    c#只能继承一个基类和多个接口(0+) 父类:Human: class Human { public virtual Move() { Console.WriteLine("Human的虚方 ...