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): """节点""" ...
随机推荐
- cookie httponly属性
Marks the cookie as accessible only through the HTTP protocol. This means that the cookie won't be a ...
- eclipse控台不见
- [读书笔记]Java之静态分派
以下内容来自周志明的<深入理解Java虚拟机>. 静态分派和重载有关. 先看代码: public static void main(String[] args) { SuperClass ...
- 查看 并发请求数及其TCP连接状态【转】
服务器上的一些统计数据: 1)统计80端口连接数netstat -nat|grep -i "80"|wc -l 2)统计httpd协议连接数ps -ef|grep httpd|wc ...
- HttpClient封装工具类
import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.List; ...
- python ABC
因为项目需要,总是会有各种各样要重命名文件的场合,manual的方法当然不可取,bat的方法又感觉不够强大,所以就从零开始学python,就为了能够自动批量修改文件名,倒腾了一个周六,总算可以了 :) ...
- 使用vue给导航栏添加链接
如下面的导航栏,使用vue技术给该导航栏增加链接: js代码为: navigation:function(){ new Vue({ el: '#navUl', data: { menuData:{ ' ...
- Unity中Instantiate一个prefab时需要注意的问题
在调用Instantiate()方法使用prefab创建对象时,接收Instantiate()方法返回值的变量类型必须和声明prefab变量的类型一致,否则接收变量的值会为null. 比如说,我在 ...
- 总结.NET 中什么时候用 Static
静态类和类成员用于创建无需创建类的实例就能够访问的数据和函数.当类中没有依赖对象标识的数据或行为时,就可以使用静态类.静态类成员是可独立于任何对象标识的数据和行为,即无论对象发生什么更改,这些数据和函 ...
- Python set集合类型操作总结
Python中除了字典,列表,元组还有一个非常好用的数据结构,那就是set了,灵活的运用set可以减去不少的操作(虽然set可以用列表代替) 小例子 1.如果我要在许多列表中找出相同的项,那么用集合是 ...