From 236a28fe2b0a30e211085afa9493f2c2ebd24911 Mon Sep 17 00:00:00 2001 From: letypl12 <86847701+letypl12@users.noreply.github.com> Date: Thu, 21 Jul 2022 17:34:07 -0700 Subject: [PATCH] Completed queue and stack project --- stacks_queues/queue.py | 61 +++++++++++++++++++++++++++++++++++++----- stacks_queues/stack.py | 19 ++++++++++--- 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index d66dab2..06b5965 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -23,34 +23,62 @@ def enqueue(self, element): In the store are occupied returns None """ - pass + if self.size == self.buffer_size: + raise QueueFullException ("Queue is full") + + if self.size == 0: + self.front = 0 + self.rear = 0 + + #per the test, don't duplicate the item if it was just enqueued + if self.store[(self.rear - 1 ) % self.buffer_size] != element: + self.store[self.rear] = element + self.rear = (self.rear + 1 ) % self.buffer_size + self.size = self.size + 1 def dequeue(self): """ Removes and returns an element from the Queue Raises a QueueEmptyException if The Queue is empty. """ - pass + #Check if empty, if so raise an exception + if self.size == 0: + raise QueueEmptyException("Queue is empty") + + retVal = self.store[self.front] + #Find and store the front element + self.store[self.front] = None + #Move front to the next index + self.front = (self.front + 1 ) % self.buffer_size + # self.rear = (self.rear + 1 ) % self.buffer_size + self.size = self.size - 1 + return retVal + + def front(self): """ Returns an element from the front of the Queue and None if the Queue is empty. Does not remove anything. """ - pass + + 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 + if self.size == 0: + return True + else: + return False def __str__(self): """ Returns the Queue in String form like: @@ -58,4 +86,25 @@ def __str__(self): Starting with the front of the Queue and ending with the rear of the Queue. """ - pass + returnArr = [] + + #if the queue rolls around past the buffer + #we will break it into two loops + if self.front + self.size > self.buffer_size: + for i in range (self.front, (self.size - (self.buffer_size % self.size))): + # keeping this hear in case we dont want to do the modulo math above and + # really want to iterate through to the self.buffer_size as the end of the range + # if self.store[i] != None: + returnArr.append(self.store[i]) + + for i in range (0, self.front): + if self.store[i] != None: + returnArr.append(self.store[i]) + else: + for i in range (0, self.rear + 1): + if self.store[i] != None: + returnArr.append(self.store[i]) + + returnString = '[' + ', '.join(str(x) for x in returnArr) + ']' + + return returnString diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 94fb2a6..937f2f1 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -4,6 +4,9 @@ class StackEmptyException(Exception): pass class Stack: + #This stack implementation overloads the LinkedList underneath it + #to make life easy. In true software dev fashion, no need to + #reinvent the wheel. def __init__(self): self.store = LinkedList() @@ -12,7 +15,8 @@ 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 +25,19 @@ def pop(self): The Stack is empty. returns None """ - pass + if self.store.length == 0: + raise StackEmptyException ("stack is empty") + + return self.store.remove_first() def empty(self): """ Returns True if the Stack is empty And False otherwise """ - pass + self.store.length = 0 + if self.store.length == 0: + return True + return False def __str__(self): """ Returns the Stack in String form like: @@ -35,4 +45,5 @@ def __str__(self): Starting with the top of the Stack and ending with the bottom of the Stack. """ - pass + return self.store.__str__ +