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): """节点""" ...
随机推荐
- 关于Ciarlet的泛函的一道homework的一个想法
[转载请注明出处]http://www.cnblogs.com/mashiqi 2016/11/21 有一道题是证明$(\mathbb{R}^n,\|\cdot\|_p)$当$p : 1< p ...
- TableView分割线从顶端开始
如果什么都不设置的话 分割线是从cell.textlabel处开始的 如果加上 [_myTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0) ...
- C语言数据结构之 简单选择排序
算法:设所排序序列的记录个数为n.i取1,2,-,n-1,从所有n-i+1个记录(Ri,Ri+1,-,Rn)中找出排序码最小的记录,与第i个记录交换.执行n-1趟 后就完成了记录序列的排序. 编译器: ...
- gulp使用
卸载插件:npm uninstall <name> [--save-dev]使用npm更新插件:npm update <name> [--save-dev]更新全部插件:npm ...
- Enum.GetHashCode()的问题
先说一下,正常如果代码可以定义成枚举,我是比较倾向于定义成枚举的,类似这样: public enum Gender { /// <summary> /// 男 /// </summa ...
- HTML5学堂,感谢您一年的陪伴(上)
在HTML学堂将满一周岁之际,感谢再过去的一年里支持和关注它的每一个小伙伴.有了你们的支持,HTML5学堂才能更好的走下去.我们将会把这一年的积累重新体现在HTML5学堂的官网上.HTML5学堂将会全 ...
- 使用Ogre::ManualObject 绘制自定义图形
在ogre中如果需要进行自定义图形绘制可以使用ManualObject.例如绘制一个三角形的用法如下: SceneNode* pGridNode = m_pBaseNode->createChi ...
- 写好unit test的建议和例子
最近翻了下写unit test 的文章,总结如下 What's unit test? "Unit testing is a software testing method by which ...
- c++句柄设计
句柄,也称为智能指针. 我计算了一下我的时间,以后每14天得读完一本书,才不愧对我买的这么多书.然而我还要抽出时间来谢谢博文.最近读的是c++沉思录,开篇就用了3章来讲述句柄.好了,废话少说,接下来谈 ...
- 激!QTREE系列
我现在才开始刷 QTREE 是不是太弱了?算了不管他…… QTREE: 树链剖分裸题(据说 lct 会超时……该说是真不愧有 spoj 的气息吗?) #include <cstdio> # ...