Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''Tehty Katariina Ikävalkon kanssa. Opiskelijanumero 525585'''
- class ListNode:
- """
- The LinkedList uses ListNode objects to store added values.
- This class will not be tested by the grader.
- Attributes:
- obj: Any object that need to be stored.
- follower: A ListNode object that follows this (self) ListNode object
- in the linked list.
- predecessor: A ListNode object that precedes this (self) ListNode object
- in the linked list.
- """
- def __init__(self, obj):
- """Initialize a list node object with the value obj."""
- self.obj = obj
- self.follower = None
- self.predecessor = None
- def add_after(self, node):
- """Adds node 'node' as the follower of this node."""
- tmp = self.follower
- self.follower = node
- node.predecessor = self
- node.follower = tmp
- if tmp:
- tmp.predecessor = node
- def remove_after(self):
- """Removes the follower of this node."""
- if self.follower:
- self.follower = self.follower.follower
- if self.follower:
- self.follower.predecessor = self
- class LinkedList:
- """
- An implementation of a doubly linked list that uses ListNode objects
- to represent nodes in the list. List indexes start from zero.
- The list contains one head and one tail guardian node with the values None.
- These can be used to check if the head or tail has been reached.
- The guardian nodes should not be included when counting the size of the list.
- """
- def __init__(self):
- """Initialize the linked list."""
- self.ListNode = ListNode
- self.head = self.ListNode(None)
- self.tail = self.ListNode(None)
- # An empty list should only have one head node followed by a tail node
- self.head.add_after(self.tail)
- def _get_at(self, n):
- """Return the node at position 'n'."""
- ln = self.head
- ln = ln.follower()
- if ln == self.tail:
- return NULL
- i=0
- while i<n:
- i=i+1
- ln = ln.follower()
- if ln == self.tail:
- return NULL
- return ln
- def add_first(self, obj):
- """Add the object 'obj' as the first node."""
- self.head.add_after(ListNode(obj))
- def add_last(self, obj):
- """Add the object 'obj' as the last node."""
- self.tail.predecessor().add_after(ListNode(obj))
- def add_position(self, n, obj):
- """Insert the object 'obj' as the 'n'th node."""
- a = _get_at(n)
- if a == None:
- add_last(obj)
- else:
- a.predecessor().add_after(LinkedList(obj))
- def remove_position(self, n):
- """Remove the node at the 'n'th position."""
- predecessor = self._get_at(n).predecessor
- if predecessor:
- predecessor.remove_after()
- def get_position(self, n):
- """Return the value of the node at the 'n'th position or None
- if there is no node at that position."""
- node = self._get_at(n)
- return node.obj if node else None
- def get_size(self):
- """Return the number of objects in the list."""
- ln = self.head
- ln = ln.follower()
- i=0
- while ln != self.tail:
- i=i+1
- ln = ln.follower()
- return i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement