diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index d66dab2..51f8c0d 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -1,61 +1,98 @@ - INITIAL_QUEUE_SIZE = 20 + class QueueFullException(Exception): pass + class QueueEmptyException(Exception): pass -class Queue: +class Queue: def __init__(self): self.store = [None] * INITIAL_QUEUE_SIZE self.buffer_size = INITIAL_QUEUE_SIZE self.front = -1 self.rear = -1 self.size = 0 - def enqueue(self, element): - """ Adds an element to the Queue - Raises a QueueFullException if all elements - In the store are occupied - returns None + """Adds an element to the Queue + Raises a QueueFullException if all elements + In the store are occupied + returns None """ - pass + if self.size == len(self.store): + raise QueueFullException + + if self.front == -1: + self.front = 0 + self.rear = 0 + self.store[self.rear] = element + + elif self.rear == len(self.store) - 1: + self.rear = 0 + self.store[self.rear] = element + else: + self.rear += 1 + self.store[self.rear] = element + self.size += 1 def dequeue(self): - """ Removes and returns an element from the Queue - Raises a QueueEmptyException if - The Queue is empty. + """Removes and returns an element from the Queue + Raises a QueueEmptyException if + The Queue is empty. """ - pass + if self.empty(): + raise QueueEmptyException + + element = self.store[self.front] + self.store[self.front] = None + + if self.front == self.rear: + self.front = -1 + self.rear = -1 + + elif self.front == len(self.store) - 1: + self.front = 0 + else: + self.front += 1 + 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. + """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 self.store[self.front] def size(self): - """ Returns the number of elements in - The Queue + """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. + """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: - [3, 4, 7] - Starting with the front of the Queue and - ending with the rear of the Queue. + """Returns the Queue in String form like: + [3, 4, 7] + Starting with the front of the Queue and + ending with the rear of the Queue. """ - pass + q = [] + start = self.front + while len(q) < self.size: + q.append(self.store[start]) + if start < len(self.store) - 1: + start += 1 + else: + start = 0 + return str(q) diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 94fb2a6..3226b18 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -1,38 +1,39 @@ from stacks_queues.linked_list import LinkedList + class StackEmptyException(Exception): pass -class Stack: +class Stack: def __init__(self): self.store = LinkedList() def push(self, element): - """ Adds an element to the top of the Stack. - Returns None + """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 - Of the Stack - Raises a StackEmptyException if - The Stack is empty. - returns None + """Removes an element from the top + Of the Stack + Raises a StackEmptyException if + The Stack is empty. + returns None """ - pass + return self.store.remove_first() if not self.empty() else StackEmptyException() def empty(self): - """ Returns True if the Stack is empty - And False otherwise + """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: - [3, 4, 7] - Starting with the top of the Stack and - ending with the bottom of the Stack. + """Returns the Stack in String form like: + [3, 4, 7] + Starting with the top of the Stack and + ending with the bottom of the Stack. """ - pass + return self.store.visit()