1. class Node():
  2. def __init__(self,InitDate):
  3. self.Date=InitDate
  4. self.next=None
  5. def setNext(self,newnext):
  6. self.next=newnext
  7. def setDate(self,newDate):
  8. self.Date=newDate
  9. def getNext(self):
  10. return self.next
  11. def getDate(self):
  12. return self.Date
  13. class LinkedList():
  14. def __init__(self):
  15. self.head=None
  16. def isEmpty(self):
  17. return self.head==None
  18. def add(self,item):
  19. temp=Node(item)
  20. temp.setNext(self.head)
  21. self.head=temp
  22. def size(self):
  23. current=self.head
  24. count=0
  25. while(current!=None):
  26. count+=1
  27. current=current.getNext()
  28. return count
  29. def show(self):
  30. current=self.head
  31. while(current!=None):
  32. print current.getDate(),
  33. current=current.getNext()
  34. print " "
  35. def search(self,item):
  36. current=self.head
  37. found=False
  38. while not found and (current != None):
  39. if current.getDate()==item:
  40. found=True
  41. else:
  42. current=current.getNext()
  43. print found
  44. def remove(self,item):
  45. previous=None
  46. current=self.head
  47. found=False
  48. while not found and (current != None):
  49. if current.getDate()==item:
  50. found=True
  51. else:
  52. previous=current
  53. current=current.getNext()
  54. if found==False:
  55. print "not {0}".format(item)
  56. elif current==self.head:
  57. self.head=current.getNext()
  58. else:
  59. previous.setNext(current.getNext())
  60. def insert(self,index,item):
  61. previous=None
  62. current=self.head
  63. count=0
  64. temp=Node(item)
  65. if index>self.size():
  66. print "out index"
  67. elif index==0:
  68. temp.setNext(current)
  69. self.head=temp
  70. else:
  71. while index:
  72. index-=1
  73. previous=current
  74. current=current.getNext()
  75. previous.setNext(temp)
  76. temp.setNext(current)
  77. if __name__=="__main__":
  78. alist=LinkedList()
  79. for i in range(10):
  80. alist.add(i)
  81. alist.show()
  82. print alist.size()
  83. alist.remove(5)
  84. alist.show()
  85. alist.insert(7,110)
  86. alist.show()
  87. 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. 关于Ciarlet的泛函的一道homework的一个想法

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2016/11/21 有一道题是证明$(\mathbb{R}^n,\|\cdot\|_p)$当$p : 1< p ...

  2. TableView分割线从顶端开始

    如果什么都不设置的话 分割线是从cell.textlabel处开始的 如果加上 [_myTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0) ...

  3. C语言数据结构之 简单选择排序

    算法:设所排序序列的记录个数为n.i取1,2,-,n-1,从所有n-i+1个记录(Ri,Ri+1,-,Rn)中找出排序码最小的记录,与第i个记录交换.执行n-1趟 后就完成了记录序列的排序. 编译器: ...

  4. gulp使用

    卸载插件:npm uninstall <name> [--save-dev]使用npm更新插件:npm update <name> [--save-dev]更新全部插件:npm ...

  5. Enum.GetHashCode()的问题

    先说一下,正常如果代码可以定义成枚举,我是比较倾向于定义成枚举的,类似这样: public enum Gender { /// <summary> /// 男 /// </summa ...

  6. HTML5学堂,感谢您一年的陪伴(上)

    在HTML学堂将满一周岁之际,感谢再过去的一年里支持和关注它的每一个小伙伴.有了你们的支持,HTML5学堂才能更好的走下去.我们将会把这一年的积累重新体现在HTML5学堂的官网上.HTML5学堂将会全 ...

  7. 使用Ogre::ManualObject 绘制自定义图形

    在ogre中如果需要进行自定义图形绘制可以使用ManualObject.例如绘制一个三角形的用法如下: SceneNode* pGridNode = m_pBaseNode->createChi ...

  8. 写好unit test的建议和例子

    最近翻了下写unit test 的文章,总结如下 What's unit test? "Unit testing is a software testing method by which ...

  9. c++句柄设计

    句柄,也称为智能指针. 我计算了一下我的时间,以后每14天得读完一本书,才不愧对我买的这么多书.然而我还要抽出时间来谢谢博文.最近读的是c++沉思录,开篇就用了3章来讲述句柄.好了,废话少说,接下来谈 ...

  10. 激!QTREE系列

    我现在才开始刷 QTREE 是不是太弱了?算了不管他…… QTREE: 树链剖分裸题(据说 lct 会超时……该说是真不愧有 spoj 的气息吗?) #include <cstdio> # ...