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. C++类成员布局

    在C++中对象的内存布局与类成员声明的顺序一致,静态成员放在数据区(Data Section)而非对象内存中,若多个类静态成员名称相同,C++则按照name mangling技术进行重命名保证名称的唯 ...

  2. K-Means 聚类算法原理分析与代码实现

    前言 在前面的文章中,涉及到的机器学习算法均为监督学习算法. 所谓监督学习,就是有训练过程的学习.再确切点,就是有 "分类标签集" 的学习. 现在开始,将进入到非监督学习领域.从经 ...

  3. js实现图片实时预览

    注: 此博客转自 http://www.cnblogs.com/goody9807/p/6064582.html  转载请注明出处 <body> 上传图片: <input type= ...

  4. VS2010调试C程序,总是一闪而过

    今天在vs2010调试C语言程序的时候,一闪而过,百度上搜了三种解决的方法,都是可以用的. 1.   #include<iostream> using namespace std; int ...

  5. Android添加快捷方式

    private void addShortcutToDesktop() { Intent shortcut = new Intent("com.android.launcher.action ...

  6. 硬件抽象层:HAL

    本章主要讲硬件抽象层:HAL,它是建立在Linux驱动之上的一套程序库.刚开始介绍了为什么要在Android中加入HAL,目的有三个,一,统一硬件的调用接口.二,解决了GPL版权问题.三,针对一些特殊 ...

  7. Office word 2013中直接调用MathType的方法

    Office word 2013中直接调用MathType的方法 | 浏览:4403 | 更新:2014-02-20 14:45 | 标签: word 使用Office word 2013的用户肯定早 ...

  8. C#的提交表单方式主要有两种WebClient与HttpWebRequest

    根据黄聪:C#模拟网站页面POST数据提交表单(转) using System; using System.Collections.Generic; using System.IO; using Sy ...

  9. linux下安装nodejs

    之前安装过windows下的node,感觉还是很方便的,不成想今天安装linux下的坑了老半天,特此记录. 1. 下载node.js,官方有提供源码版本和编译版的,方便起见我使用编译版的,下载后解压缩 ...

  10. lua判断表中数据是否连续,0可以代表任何数

    最近看到有lua面试题,挺有意思的,一张表中有若干个数,0可以代表任何数 比如有张表{9, 2, 4, 1, 3, 0, 0, 0, 0},按照规则这张表中的四个0可以用来代表5,6,7,8,那么这张 ...