diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index d66dab2..37cb741 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -17,40 +17,55 @@ def __init__(self): self.size = 0 + # methods to add to the Queue class def enqueue(self, element): """ Adds an element to the Queue Raises a QueueFullException if all elements In the store are occupied returns None """ - pass + if self.size == self.buffer_size: + raise QueueFullException + self.store[self.rear] = element + self.rear = (self.rear + 1) % self.buffer_size + self.size += 1 + def dequeue(self): """ Removes and returns an element from the Queue Raises a QueueEmptyException if The Queue is empty. """ - pass + if self.size == 0: + raise QueueEmptyException + element = self.store[self.front] + self.front = (self.front + 1) % self.buffer_size + self.size -= 1 + return element + + def front(self): """ Returns an element from the front of the Queue and None if the Queue is empty. Does not remove anything. """ - pass + if self.front == -1: + return None + return self.store[self.front] def size(self): """ Returns the number of elements in The Queue """ - pass + return self.size def empty(self): """ Returns True if the Queue is empty And False otherwise. """ - pass + return self.size == 0 def __str__(self): """ Returns the Queue in String form like: @@ -58,4 +73,9 @@ def __str__(self): Starting with the front of the Queue and ending with the rear of the Queue. """ - pass + values = [] + index = self.front + for i in range(0, self.size): + values.append(str(self.store[index])) + index = (index + 1) % self.buffer_size + return "[" + ", ".join(values) + "]" \ No newline at end of file diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 94fb2a6..5c08c27 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -1,3 +1,4 @@ +from tokenize import String from stacks_queues.linked_list import LinkedList class StackEmptyException(Exception): @@ -7,12 +8,15 @@ class Stack: def __init__(self): self.store = LinkedList() + self.front = -1 + self.rear = -1 + self.size = 0 def push(self, element): """ Adds an element to the top of the Stack. Returns None """ - pass + self.store.add_first(element) def pop(self): """ Removes an element from the top @@ -21,13 +25,13 @@ def pop(self): The Stack is empty. returns None """ - pass + return self.store.remove_first() def empty(self): """ Returns True if the Stack is empty And False otherwise """ - pass + return self.store.empty() def __str__(self): """ Returns the Stack in String form like: @@ -35,4 +39,5 @@ def __str__(self): Starting with the top of the Stack and ending with the bottom of the Stack. """ - pass + + return str(self.store) \ No newline at end of file