diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index d66dab2..4fd51f9 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -1,5 +1,6 @@ INITIAL_QUEUE_SIZE = 20 +RESIZE_INCREMENT = 5 class QueueFullException(Exception): pass @@ -13,8 +14,9 @@ def __init__(self): self.store = [None] * INITIAL_QUEUE_SIZE self.buffer_size = INITIAL_QUEUE_SIZE self.front = -1 - self.rear = -1 + self.back = -1 self.size = 0 + self.resize_increment = RESIZE_INCREMENT def enqueue(self, element): @@ -23,34 +25,55 @@ def enqueue(self, element): In the store are occupied returns None """ - pass + if self.size == self.buffer_size: + self.__enlarge_capacity(self.resize_increment) + + if self.front == -1: + self.front = 0 + self.back = 0 + + self.store[self.back] = element + self.back = (self.back + 1) % len(self.store) + 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.empty(): + raise QueueEmptyException + element = self.store[self.front] + self.front = (self.front + 1) % len(self.store) + 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.empty(): + 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 +81,120 @@ def __str__(self): Starting with the front of the Queue and ending with the rear of the Queue. """ - pass + return str(self.__get_queue()) + + def __enlarge_capacity(self, i): + queue = self.__get_queue() + + self.store = queue + ([None] * i) + self.buffer_size += i + self.front = 0 + self.back = len(queue) + + def __get_queue(self): + queue = [] + if self.front < self.back: + for i in range(self.front, (self.back) % len(self.store)): + queue.append(self.store[i]) + + if self.back <= self.front: + for i in range(self.front, len(self.store)): + queue.append(self.store[i]) + for j in range(self.back): + queue.append(self.store[j]) + + return queue + + + +# INITIAL_QUEUE_SIZE = 20 + +# class QueueFullException(Exception): +# pass + +# class QueueEmptyException(Exception): +# pass + +# class Queue: + +# def __init__(self): +# self.store = [None] * INITIAL_QUEUE_SIZE +# self.buffer_size = INITIAL_QUEUE_SIZE +# self.front = -1 +# self.back = -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 +# """ +# if self.size == self.buffer_size: +# raise QueueFullException + +# if self.front == -1: +# self.front = 0 +# self.back = 0 + +# self.store[self.back] = element +# self.back = (self.back + 1) % len(self.store) +# self.size += 1 + + +# def dequeue(self): +# """ Removes and returns an element from the Queue +# Raises a QueueEmptyException if +# The Queue is empty. +# """ +# if self.empty(): +# raise QueueEmptyException + +# element = self.store[self.front] +# self.front = (self.front + 1) % len(self.store) +# 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. +# """ +# if self.empty(): +# return None + +# return self.store[self.front] + + +# def size(self): +# """ Returns the number of elements in +# The Queue +# """ +# return self.size + +# def empty(self): +# """ Returns True if the Queue is empty +# And False otherwise. +# """ +# 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. +# """ +# queue = [] +# if self.front < self.back: +# for i in range(self.front, (self.back) % len(self.store)): +# queue.append(self.store[i]) + +# if self.back <= self.front: +# for i in range(self.front, len(self.store)): +# queue.append(self.store[i]) +# for j in range(self.back): +# queue.append(self.store[j]) +# return str(queue) \ No newline at end of file diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 94fb2a6..e136fb6 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -12,22 +12,25 @@ 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 Of the Stack Raises a StackEmptyException if The Stack is empty. - returns None + returns removed element """ - pass + if self.store.empty(): + raise StackEmptyException + + return self.store.remove_first() def empty(self): """ Returns True if the Stack is empty And False otherwise """ - pass + return not self.store.head def __str__(self): """ Returns the Stack in String form like: @@ -35,4 +38,11 @@ def __str__(self): Starting with the top of the Stack and ending with the bottom of the Stack. """ - pass + values = [] + + current = self.store.head + while current: + values.append(str(current.value)) + current = current.next + + return ", ".join(values) diff --git a/tests/test_queue.py b/tests/test_queue.py index 7887eba..9496413 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -57,5 +57,7 @@ def test_with_large_queue(queue): assert str(queue) == "[30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220]" - with pytest.raises(QueueFullException): - queue.enqueue('This will break it') \ No newline at end of file + for num in [230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350]: + queue.enqueue(num) + + assert str(queue) == "[30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350]" \ No newline at end of file