Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. class Node:
  2. def __init__(self,data=None,next=None):
  3. self.data = data
  4. self.next = next
  5.  
  6. def __str__(self):
  7. return "Node[Data=" + `self.data` + "]"
  8.  
  9.  
  10. class LinkedList:
  11. def __init__(self):
  12. self.head = None
  13. self.count = 0
  14.  
  15. def insert(self,data):
  16. if self.head == None:
  17. self.head = Node(data)
  18. else:
  19. current = self.head
  20. while current.next is not None:
  21. current = current.next
  22. current.next = Node(data)
  23. self.count += 1
  24.  
  25. def delete2(self, data):
  26. current = self.head
  27. if current.data == data:
  28. self.head = current.next
  29. if current is None:
  30. return False
  31. else:
  32. while (current is not None) and (current.next is not None) and (current.next.data is not data):
  33. current = current.next
  34. if (current is not None) and (current.next is not None):
  35. current.next = current.next.next
  36.  
  37. def delete_backup(self, data):
  38. current = self.head
  39.  
  40. if current.next is None:
  41. self.head = current.next
  42.  
  43. if current is None:
  44. current.data = None
  45. else:
  46. while (current is not None) and (current.next is not None) and (current.next.data is not data):
  47. current = current.next
  48. if (current is not None) and (current.next is not None):
  49. current.next = current.next.next
  50.  
  51. def count(self):
  52. i = 0
  53. print "There are ", str(self.count), " items in the list"
  54.  
  55. def delete(self):
  56. current = self.head
  57. if current.next is None:
  58. self.head = None
  59. else:
  60. while current.next.next is not None:
  61. current = current.next
  62. else:
  63. current.next = None
  64. print "Item deleted from stack"
  65.  
  66. def find(self,data):
  67. found = False
  68. current = self.head
  69. while ((current is not None) and (current.data is not data) and ( current.next is not None)):
  70. current = current.next
  71. if ((current.data == data)):
  72. found = True
  73. return found
  74.  
  75. def display(self):
  76. count = 0
  77. current = self.head
  78. string_representation = " "
  79. while current is not None:
  80. count += 1
  81. string_representation += str(current) + "--->"
  82. current = current.next
  83. print string_representation
  84. print count
  85. ll = LinkedList()
  86.  
  87. ll.insert(1)
  88. ll.insert(2)
  89. ll.insert(3)
  90. ll.insert(9)
  91. ll.insert(2)
  92. ll.insert(6)
  93.  
  94. ll.display()
  95.  
  96. ll.delete()
  97.  
  98. ll.display()
  99.  
  100. ll.count()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement