Double Linked List

  1. # NodeModule.py
  2. #!/usr/bin/python
  3. """ A model containing Node class """
  4.  
  5. __author__ = "Lin Chen"
  6. __version__ = "0.1"
  7.  
  8. class Node:
  9. """Single node with double pointers pointing to its previous node and the node after it"""
  10.  
  11. def __init__(self, data):
  12. """Initialize a Node
  13. Args:
  14. data : data saved in the node
  15. """
  16. self._data = data
  17. self._prev = None
  18. self._next = None
  19.  
  20. @property
  21. def data(self):
  22. return self._data
  23.  
  24. @data.setter
  25. def data(self, d):
  26. self._data = d
  27.  
  28. @data.deleter
  29. def data(self):
  30. del self._data
  31.  
  32. @property
  33. def next(self):
  34. return self._next
  35.  
  36. @next.setter
  37. def next(self, n):
  38. self._next = n
  39.  
  40. @next.deleter
  41. def next(self):
  42. del self._next
  43.  
  44. @property
  45. def prev(self):
  46. return self._prev
  47.  
  48. @prev.setter
  49. def prev(self, n):
  50. self._prev = n
  51.  
  52. @prev.deleter
  53. def prev(self):
  54. del self._prev
  55.  
  56. def __str__(self):
  57. return str(self._data)
  1. # ListModule.py
  2. #!/usr/bin/python
  3. """ A modeule containing List class"""
  4.  
  5. from NodeModule import Node
  6.  
  7. class List:
  8. """Linked List class with a pre-defined Node class"""
  9.  
  10. def __init__(self):
  11. """Initialize a List
  12. """
  13. self._head = None
  14. self._tail = None
  15. self._count = 0
  16.  
  17. def getSize(self):
  18. """Return the size of the list
  19. Returns:
  20. int : size of the list
  21. """
  22. return self._count
  23.  
  24. def insert(self, index, v):
  25. """Insert a valude to make the list maintain ascending order
  26.  
  27. Args:
  28. v : data which can be saved in a node
  29.  
  30. Returns:
  31. boolean: True, if the value is inserted succefully; False, otherwise
  32. """
  33. if index < 0 or index > self._count:
  34. return False
  35.  
  36. n = Node(v) # create a node
  37.  
  38. # insert a node into an empty list
  39. if self.getSize() == 0:
  40. self._head = n
  41. self._tail = n
  42. self._count += 1
  43. return True
  44.  
  45. # insert a node in the front
  46. if index == 0:
  47. n.next = self._head
  48. self._head.prev = n
  49. self._head = n
  50. self._count += 1
  51. return True
  52.  
  53. # insert a node at the end
  54. if index == self.getSize():
  55. n.prev = self._tail
  56. self._tail.next = n
  57. self._tail = n
  58. self._count += 1
  59. return True
  60.  
  61. # insert in the first half
  62. if index <= self.getSize()/2:
  63. current = self._head
  64. for i in range(index-1):
  65. current = current.next
  66. n.next = current.next
  67. n.prev = current
  68. current.next.prev = n
  69. current.next = n
  70. self._count += 1
  71. return True
  72. # insert a node in the second half
  73. else:
  74. current = self._tail
  75. for i in range(self.getSize()-index-1):
  76. current = current.prev
  77. n.next = current
  78. n.prev = current.prev
  79. current.prev.next = n
  80. current.prev = n
  81. self._count += 1
  82. return True
  83.  
  84. return False
  85.  
  86. def delete(self, index):
  87. """Delete a valude located at a specific location in the list
  88.  
  89. Args:
  90. index (int): location of deletion, 0 is the location before the first element, 1 is the location after the first element
  91.  
  92. Returns:
  93. boolean: True, if the value is deleted succefully; False, otherwise
  94. """
  95. if index < 0 or index > self._count-1:
  96. return False
  97.  
  98. # delete the first node
  99. if index == 0:
  100. self._head = self._head.next
  101. self._head.prev = None
  102. self._count -= 1
  103. return True
  104.  
  105. # delete the last node
  106. if index == self._count-1:
  107. self._tail = self._tail.prev
  108. self._tail.next = None
  109. self._count -= 1
  110. return True
  111.  
  112. # delete a node located in the first half
  113. if index <= self._count/2:
  114. current = self._head
  115. for i in range(index-1):
  116. current = current.next
  117. current.next = current.next.next
  118. current.next.prev = current
  119. self._count -= 1
  120. return True
  121. # delete a node located in the second half
  122. else:
  123. current = self._tail
  124. for i in range(self.getSize()-index-2):
  125. current = current.prev
  126. current.prev = current.prev.prev
  127. current.prev.next = current
  128. self._count -= 1
  129. return True
  130.  
  131. return False
  132.  
  133. def __str__(self):
  134. """Convert the list to a string
  135.  
  136. Returns:
  137. string : a string represents the list
  138. """
  139. if self.getSize() == 0:
  140. return "Empty"
  141.  
  142. current = self._head
  143. output = []
  144.  
  145. while current is not None:
  146. output.append(str(current))
  147. current = current.next
  148.  
  149. return " -> ".join(output)
  1. # Test.py
  2. #!/usr/bin/python
  3.  
  4. from ListModule import List
  5.  
  6. def main():
  7. l = List()
  8.  
  9. l.insert(0, 20) # insert the first node
  10. l.insert(1, 10) # insert to the end
  11. l.insert(0, 40) # insert to the front
  12. l.insert(2, 30) # insert to the second half
  13. l.insert(2, 50) # insert to the first half
  14. print(l) # 40 -> 20 -> 50 -> 30 -> 10
  15.  
  16. # insert more nodes
  17. l.insert(0, 60)
  18. l.insert(0, 70)
  19. l.insert(0, 80)
  20. print(l) # 80 -> 70 -> 60 -> 40 -> 20 -> 50 -> 30 -> 10
  21.  
  22. l.delete(2) # delete a node in the first half
  23. print(l) # 80 -> 70 -> 40 -> 20 -> 50 -> 30 -> 10
  24. l.delete(4) # delete a node in the second half
  25. print(l) # 80 -> 70 -> 40 -> 20 -> 30 -> 10
  26. l.delete(0) # delete the first node
  27. print(l) # 70 -> 40 -> 20 -> 30 -> 10
  28. l.delete(4) # delete the last node
  29. print(l) # 70 -> 40 -> 20 -> 30
  30.  
  31.  
  32. if __name__ == '__main__':
  33. main()