Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1.  
  2.  
  3. '''Tehty Katariina Ikävalkon kanssa. Opiskelijanumero 525585'''
  4. class ListNode:
  5. """
  6. The LinkedList uses ListNode objects to store added values.
  7. This class will not be tested by the grader.
  8.  
  9. Attributes:
  10. obj: Any object that need to be stored.
  11. follower: A ListNode object that follows this (self) ListNode object
  12. in the linked list.
  13. predecessor: A ListNode object that precedes this (self) ListNode object
  14. in the linked list.
  15. """
  16. def __init__(self, obj):
  17. """Initialize a list node object with the value obj."""
  18. self.obj = obj
  19. self.follower = None
  20. self.predecessor = None
  21.  
  22. def add_after(self, node):
  23. """Adds node 'node' as the follower of this node."""
  24. tmp = self.follower
  25. self.follower = node
  26. node.predecessor = self
  27. node.follower = tmp
  28. if tmp:
  29. tmp.predecessor = node
  30.  
  31. def remove_after(self):
  32. """Removes the follower of this node."""
  33. if self.follower:
  34. self.follower = self.follower.follower
  35. if self.follower:
  36. self.follower.predecessor = self
  37.  
  38.  
  39. class LinkedList:
  40. """
  41. An implementation of a doubly linked list that uses ListNode objects
  42. to represent nodes in the list. List indexes start from zero.
  43.  
  44. The list contains one head and one tail guardian node with the values None.
  45. These can be used to check if the head or tail has been reached.
  46. The guardian nodes should not be included when counting the size of the list.
  47. """
  48. def __init__(self):
  49. """Initialize the linked list."""
  50. self.ListNode = ListNode
  51. self.head = self.ListNode(None)
  52. self.tail = self.ListNode(None)
  53. # An empty list should only have one head node followed by a tail node
  54. self.head.add_after(self.tail)
  55.  
  56. def _get_at(self, n):
  57. """Return the node at position 'n'."""
  58. ln = self.head
  59. ln = ln.follower()
  60. if ln == self.tail:
  61. return NULL
  62. i=0
  63. while i<n:
  64. i=i+1
  65. ln = ln.follower()
  66. if ln == self.tail:
  67. return NULL
  68. return ln
  69.  
  70.  
  71. def add_first(self, obj):
  72. """Add the object 'obj' as the first node."""
  73. self.head.add_after(ListNode(obj))
  74.  
  75.  
  76. def add_last(self, obj):
  77. """Add the object 'obj' as the last node."""
  78. self.tail.predecessor().add_after(ListNode(obj))
  79.  
  80.  
  81. def add_position(self, n, obj):
  82. """Insert the object 'obj' as the 'n'th node."""
  83. a = _get_at(n)
  84. if a == None:
  85. add_last(obj)
  86. else:
  87. a.predecessor().add_after(LinkedList(obj))
  88.  
  89.  
  90.  
  91. def remove_position(self, n):
  92. """Remove the node at the 'n'th position."""
  93. predecessor = self._get_at(n).predecessor
  94. if predecessor:
  95. predecessor.remove_after()
  96.  
  97. def get_position(self, n):
  98. """Return the value of the node at the 'n'th position or None
  99. if there is no node at that position."""
  100. node = self._get_at(n)
  101. return node.obj if node else None
  102.  
  103. def get_size(self):
  104. """Return the number of objects in the list."""
  105. ln = self.head
  106. ln = ln.follower()
  107. i=0
  108. while ln != self.tail:
  109. i=i+1
  110. ln = ln.follower()
  111. return i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement