Advertisement
Guest User

easdasd

a guest
Oct 21st, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. class LinkedList(object):
  2. __slots__ = ['__head', '__tail', '__current', '__size']
  3. class Node(object):
  4. __slots__ = ['__data', '__next']
  5. def __init__(self, data, next = None):
  6. self.__data=data
  7. self.__next=next
  8.  
  9. def getData(self):
  10. return self.__data
  11. def getNext(self):
  12. return self.__next
  13. def setData(self, data):
  14. self.__data = data
  15. def setNext(self, next):
  16. self.__next = next
  17. def __str__(self):
  18. data = str(self.__data)
  19. def __init__(self):
  20. self.__head = None
  21. self.__tail = None
  22. self.__current = None
  23. self.__size = 0
  24. def getHead(self): #: Return first node's values and "IndexError" if the list is empty
  25. if self.__head != None:
  26. return self.__head.getData()
  27. else:
  28. raise IndexError, ('Empty List')
  29.  
  30. def getTail(self):
  31. if self.__tail != None:
  32. return self.__tail.getData()
  33. else:
  34. raise IndexError,('Empty List')
  35.  
  36.  
  37. def getCurrent(self):
  38. if self.__current != None:
  39. return self.__current.getData()
  40. else:
  41. raise IndexError,('Empty List')
  42. def moveNext(self):
  43. if (self.__tail != self.__current) or (self.__head != None) :
  44. self.__current=self.__current.getNext()
  45.  
  46. else:
  47. raise IndexError,('Empty list or current node is the tail node')
  48.  
  49. def moveHead(self):
  50. if self.__head != None :
  51. self.__current=self.__head
  52. print 'hola'
  53. else:
  54. raise IndexError,('Empty list')
  55.  
  56. def isEmpty(self):
  57. return self.__head == None
  58. def size(self):
  59. return self.__size
  60. def clear(self):
  61. self.moveHead()
  62. while (self.__size != 0):
  63. self.remove()
  64. self.__current = None
  65.  
  66.  
  67.  
  68. def insertBefore(self, data):
  69. nodeAux =self.Node(data)
  70. if self.__head == None:
  71. self.__head = nodeAux
  72. self.__current = nodeAux
  73. self.__tail = nodeAux
  74. elif self.__current == self.__head:
  75. nodeAux.setNext(self.__current)
  76. self.__head==nodeAux
  77. else:
  78. nodeAux=self.__head
  79. while self.__current != nodeAux.getNext():
  80. nodeAux=nodeAux.getNext()
  81. nodeAux.setNext(self.__current)
  82. self.__current=nodeAux
  83. self.__size +=1
  84.  
  85.  
  86. def insertAfter(self, data):
  87. newNode= self.Node(data) #Create the NewNode with the data value
  88. if self.__head == None:
  89. self.__head = newNode
  90. self.__current = newNode
  91. self.__tail = newNode
  92.  
  93. elif self.__current == self.__tail:
  94. self.__current.setNext(newNode) # Now, the self.__current has to point his next "lisk" to the NewNode
  95. self.__current=newNode # Now the newMode is the TAIL node, if I use _current node to equaling it with newMode I will being moving the current node one posicion
  96. self.__tail = newNode
  97. else:
  98. newNode.setNext(self.__current.getNext()) # Link the next pointer of the NewMode to the Next of self.__current
  99. self.__current.setNext(newNode) # Now, the self.__current has to point his next "lisk" to the NewNode
  100. self.moveNext()
  101. self.__size +=1
  102.  
  103. def remove(self):
  104. removeNode=self.Node(None)
  105.  
  106. if self.__current == self.__head:
  107. self.__head = removeNode
  108. self.__current = removeNode
  109.  
  110. elif self.__current == self.__tail:
  111. self.__current = removeNode
  112. self.__tail = removeNode
  113.  
  114. elif self.__current == None and self.__head == None and self.__tail == None:
  115. raise IndexError,('Empty list, Cannot remove the NODE')
  116. else:
  117. removeNode = self.__head
  118. while self.__current != removeNode.getNext():
  119. removeNode=removeNode.getNext()
  120. self.__current=removeNode
  121. removeNode = removeNode.getNext()
  122. removeNode = None
  123.  
  124.  
  125. self.__size -=1
  126.  
  127.  
  128. def __str__(self):
  129. List = 'ListOfNodeDatas -->'
  130. recorrer = self.__head
  131. while node != None:
  132. List = List + str(recorrer.data)
  133. if recorrer.next != None:
  134. List = List + ' && '
  135. recorrer = recorrer.next
  136. return List
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement