class LinkedListQueue(QueueABC): """ Implementation of the queue data structure using a linked list for FIFO operations """ def __init__(self): self._list = LinkedList() def first(self): """ Examine and return the element at the front of the queue :return: first element :raise: QueueException if empty queue """ # if there is nothing in the queue then raise an exception if self.is_empty(): raise QueueException("Cannot fetch first from empty queue") return self._list.first.element def enqueue(self, x): """ Add an element to the back of the queue. :param x: object to add :return: :raise: TypeError if nothing is queued """ self._list.insert_last(x) def __len__(self): """ Return the length of the queue :return: number of elements """ return len(self._list) def size(self): """ Return the number of elements in the queue :return: number of elements """ return len(self._list) def dequeue(self): """ Remove and return the first element in the queue :return: first element """ # if there is nothing in the queue then raise an exception if self.is_empty(): raise QueueException("Cannot dequeue from empty queue") return self._list.delete_first() def is_empty(self): """ Return true if queue is empty else False :return: True or False """ return True if len(self._list) == 0 else False
class LinkedListStack(StackABC): """ Implementation of the stack data structure using a linked list for LIFO operations """ def __init__(self): self._list = LinkedList() def __len__(self): return self.size() def __iter__(self): for i in self._list: yield i def size(self): """ Return number of items in stack """ return len(self._list) def peek(self): """ Return the obj, but leave it on the stack :return: object :raise: StackException """ if self._list.first: return self._list.first.element else: raise StackException("Cannot peek into empty stack") def push(self, x): """ Add element x to top of stack :param x: object to push on stack :return: """ self._list.insert_first(x) def is_empty(self): """ Return True if empty stack else False :return: True/False """ return False if len(self._list) else True def pop(self): """ Remove element from top of stack and return it :return: newest object :raise: StackException """ if len(self._list): return self._list.delete_first() else: raise StackException("Cannot pop from empty stack")
class OrderedLinkedList(OrderedListABC): """ The OrderedLinkedList class implements a linked list that will insert elements based on their ordering. It expects that elements are of the same type and order-comparable. An order-comparable class defines __eq__, __ne__, __lt__, __le__, __gt__, __ge__ """ def __init__(self, element_class): """ :param element_class: class that this linked list will store """ self.element_class = element_class self._list = LinkedList() def __iter__(self): for obj in self._list: yield obj def add(self, x): """ Add an element into the list at its appropriate spot """ if not isinstance(x, self.element_class): raise ListException( "Cannot add element because is not an instance of {}".format( self.element_class.__name__)) # find where the element belongs in the order # base case, empty list if len(self._list) == 0: self._list.insert_first(x) # > 0 element else: # loop until we hit a bigger item then insert prev_node = None node = self._list.first while node: if node.element > x: break prev_node = node node = node.next_node if prev_node is None: # first item was greater than x self._list.insert_first(x) else: self._list.insert_after_node(prev_node, x) return @property def first(self): """ Examine the element at the front of the list :return: element at front """ # if there is nothing in the list then raise an exception if self.is_empty: raise ListException("Cannot fetch first element from empty list") return self._list.first.element @property def last(self): """ Examine the element at the rear of the list :return: element at rear """ # if there is nothing in the list then raise an exception if self.is_empty: raise ListException("Cannot fetch last element from empty list") return self._list.last.element def __len__(self): """ Return the length of the list :return: number of elements """ return len(self._list) @property def size(self): """ Return the number of elements in the list :return: """ return len(self._list) def remove_first(self): # if there is nothing in the list then raise an exception if self.is_empty: raise ListException("Cannot remove first from empty list") return self._list.delete_first() def remove_last(self): # if there is nothing in the list then raise an exception if self.is_empty: raise ListException("Cannot remove last from empty list") return self._list.delete_last() @property def is_empty(self): """ Return true if list is empty else False :return: True or False """ return True if self.size == 0 else False def contains(self, x): """ Return True if list contains element x, else False :param x: object to check for :return: True if in list, else False """ return self._list.search(x)