From 173613b7250ec7855fddd39ec858be1107167ff1 Mon Sep 17 00:00:00 2001 From: Angela Fan Date: Sun, 17 Jul 2022 17:51:46 -0700 Subject: [PATCH] queue missing one test, stacks passing all tests --- stacks_queues/queue.py | 37 ++++++++++++++++++++++++++++++------- stacks_queues/stack.py | 11 +++++++---- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index d66dab2..3a44bc8 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -1,4 +1,3 @@ - INITIAL_QUEUE_SIZE = 20 class QueueFullException(Exception): @@ -23,34 +22,58 @@ def enqueue(self, element): In the store are occupied returns None """ - pass + + if self.size == self.buffer_size: + raise QueueFullException() + + if self.front == -1: + self.front = self.rear = 0 + + # if self.rear == self.buffer_size: + # self.rear = 0 + + self.size += 1 + self.store[self.rear] = element + self.rear = (self.rear + 1) % self.buffer_size + 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() + + self.size -= 1 + element = self.store[self.front] + self.front += 1 + if self.front == self.buffer_size: + self.front = 0 + 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.size == 0: + return None + else: + 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 +81,4 @@ def __str__(self): Starting with the front of the Queue and ending with the rear of the Queue. """ - pass + return str(self.store[self.front:self.rear]) diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 94fb2a6..5f821c7 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -12,7 +12,7 @@ def push(self, element): """ Adds an element to the top of the Stack. Returns None """ - pass + return self.store.add_first(element) def pop(self): """ Removes an element from the top @@ -21,13 +21,16 @@ def pop(self): The Stack is empty. returns None """ - pass + if self.store.empty(): + raise StackEmptyException() + else: + 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 +38,4 @@ def __str__(self): Starting with the top of the Stack and ending with the bottom of the Stack. """ - pass + return str(self.store)