diff --git a/stacks_queues/linked_list.py b/stacks_queues/linked_list.py index 3cb0aa4..5a08330 100644 --- a/stacks_queues/linked_list.py +++ b/stacks_queues/linked_list.py @@ -48,7 +48,7 @@ def remove_first(self): def empty(self): - return not self.head + return not self.head # method to find if the linked list contains a node with specified value # returns true if found, false otherwise diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index d66dab2..57260c1 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -1,4 +1,7 @@ +from logging import raiseExceptions + + INITIAL_QUEUE_SIZE = 20 class QueueFullException(Exception): @@ -7,6 +10,11 @@ class QueueFullException(Exception): class QueueEmptyException(Exception): pass +# queue = Queue() + +# queue.enqueue(1) +# print(str(queue)) # [Object Queue] + class Queue: def __init__(self): @@ -15,22 +23,30 @@ def __init__(self): 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 - """ - pass + """ + if self.get_size() + 1 > INITIAL_QUEUE_SIZE: + raise QueueFullException("Queue is full") + + queue = self.store + if queue != None: + queue.append(element) + def dequeue(self): """ Removes and returns an element from the Queue Raises a QueueEmptyException if The Queue is empty. """ - pass + result = self.store.pop(0) + if result is None: + return self.dequeue() + return result def front(self): """ Returns an element from the front @@ -40,6 +56,13 @@ def front(self): pass + def get_size(self): + """ Returns the number of elements in + The Queue + """ + list_where_not_none = [x for x in self.store if x is not None] + return len(list_where_not_none) + def size(self): """ Returns the number of elements in The Queue @@ -50,7 +73,7 @@ def empty(self): """ Returns True if the Queue is empty And False otherwise. """ - pass + return self.get_size() == 0 def __str__(self): """ Returns the Queue in String form like: @@ -58,4 +81,5 @@ def __str__(self): Starting with the front of the Queue and ending with the rear of the Queue. """ - pass + return str([x for x in self.store if x is not None]) + diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 94fb2a6..b7bf1f8 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 + self.store.add_last(element) def pop(self): """ Removes an element from the top @@ -21,13 +21,13 @@ def pop(self): The Stack is empty. returns None """ - pass + return self.store.remove_last() def empty(self): """ Returns True if the Stack is empty And False otherwise """ - pass + return self.store.length() == 0 def __str__(self): """ Returns the Stack in String form like: