From 6ca632d294e1117bae31217ddf7b62a2685f4ec8 Mon Sep 17 00:00:00 2001 From: Lilly Date: Mon, 18 Jul 2022 20:33:43 -0700 Subject: [PATCH 1/3] Lilly C16 Pine --- stacks_queues/queue.py | 32 ++++++++++++++++++++++++++------ stacks_queues/stack.py | 9 ++++++--- 2 files changed, 32 insertions(+), 9 deletions(-) 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..af25731 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -7,12 +7,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 +24,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: From c6e44d12969ad73fd68f0bef80d12e2d0b4f9dd5 Mon Sep 17 00:00:00 2001 From: Lilly Date: Mon, 18 Jul 2022 20:57:15 -0700 Subject: [PATCH 2/3] added last, Lilly C16 Pine --- stacks_queues/stack.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index af25731..008c638 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): @@ -38,4 +39,5 @@ def __str__(self): Starting with the top of the Stack and ending with the bottom of the Stack. """ - pass + String = "[" + str(self.store.first()) + "]" + return String From 753e7cc0a32a4b57784672b5e8774592435ecec3 Mon Sep 17 00:00:00 2001 From: Lilly Date: Sat, 23 Jul 2022 06:41:14 -0700 Subject: [PATCH 3/3] resubmitt --- stacks_queues/stack.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 008c638..5c08c27 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -39,5 +39,5 @@ def __str__(self): Starting with the top of the Stack and ending with the bottom of the Stack. """ - String = "[" + str(self.store.first()) + "]" - return String + + return str(self.store) \ No newline at end of file