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. Centos6.5 gitlab安装使用

    公司从svn转到git做版本管理,我搜了一下网上git的服务器,包括gitosis,gitolite等.一开始我是用的是gitosis作为git服务器的,安装过程还算比较简单,整个服务使用python ...

  2. (原创)jQuery Media Plugin-jQuery的网页媒体播放器插件的使用心得

    jQuery Media Plugin是一款基于jQuery的网页媒体播放器插件,它支持大部分的网络多媒体播放器和多媒体格式,比如:Flash, Windows Media Player, Real ...

  3. Centos 6.7 安装smokeping (最完整教程)

    本教程需要的源码包一并上传了,届时可以直接上传到linux系统里面! 需要编译的fping.echoping.smokeping源码包,链接:http://pan.baidu.com/s/1pL4HL ...

  4. C#基础——winform应用上传图片到SQLServer数据库

    前言 之前通过winform与SQL Server的交互一直局限于文本.数字等信息,都可以通过string的方式来传输,但是比如音乐.图片等特殊格式的文件要如何与SQL Server数据库进行交互呢? ...

  5. Your stream was neither an OLE2 stream, nor an OOXML stream.问题的解决

    先说说问题的来源 ,使用NPOI读取Except,先通过流来读取,如果符合要求,就将流保存为文件. 众所周知,流只能读一次,所以在流读取之前需要将流拷贝一份,保存文件的时候使用. protected ...

  6. this其实是js的一个对象谁调用它它就指向谁

    本人看了一下,感觉对this解释的有点复杂了,因此,本人在此给this一个简单易于理解的定义. 因为上面计算出来的结果不符合我们的习惯,并且负值在计算的时候会影响正确性,现在我们给这个结果加上180 ...

  7. leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  8. 关于Ubuntu共享文件夹的设置

    一.Xshell连接虚拟机(先关闭虚拟机的防火墙) sudo apt-get install openssh-serve sudo ufw disable sudo ufw allow 22 二.虚拟 ...

  9. s2 devMode cmdshell

    s2 devMode cmdshell   仅支持批量验证,命令执行 链接:http://pan.baidu.com/s/1sl7tgRV 密码:wud8 也可以通过outscan一键获取,之后导入t ...

  10. c# CLI托管工程开启调试c++库工程代码

    启动调试c#winform工程中,无法命中c++库工程中中的断点,在c#工程中更改调试设置: 勾选上Enable unmanaged code debuging