diff --git a/stacks_queues/linked_list.py b/stacks_queues/linked_list.py index 3cb0aa4..4a47373 100644 --- a/stacks_queues/linked_list.py +++ b/stacks_queues/linked_list.py @@ -1,4 +1,3 @@ - class EmptyListError(Exception): pass diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index d66dab2..8fb33a9 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -1,11 +1,36 @@ +from typing import ItemsView + INITIAL_QUEUE_SIZE = 20 class QueueFullException(Exception): - pass + def __init__(self, *args): + if args: + self.message = args[0] + else: + self.message = None + + def __str__(self): + print('calling str') + if self.message: + return 'QueueFullException, {0}'.format(self.message) + else: + return 'QueueFullException has been raised' + class QueueEmptyException(Exception): - pass + def __init__(self, *args): + if args: + self.message = args[0] + else: + self.message = None + + def __str__(self): + print('calling str') + if self.message: + return 'QueueEmptyException, {0}'.format(self.message) + else: + return 'QueueEmptyException has been raised' class Queue: @@ -23,39 +48,97 @@ def enqueue(self, element): In the store are occupied returns None """ - pass + if ((self.rear +1) % INITIAL_QUEUE_SIZE ==self.front): + raise QueueFullException('This will break it') + + elif (self.front == -1): + self.front = 0 + self.rear = 0 + self.store[self.rear] = element + self.size = self.size + 1 + else: + + # next position of rear + self.rear = (self.rear + 1) % INITIAL_QUEUE_SIZE + self.store[self.rear] = element + self.size = self.size + 1 def dequeue(self): """ Removes and returns an element from the Queue Raises a QueueEmptyException if - The Queue is empty. + The Queue is empty. In the circular Queue it remove the item at the front """ - pass + + if self.empty(): + raise QueueEmptyException("The Stack is empty") + + # condition for only one element + elif (self.front == self.rear): + tmp=self.store[self.front] + self.front = -1 + self.rear = -1 + self.size -=1 + return tmp + + else: + tmp = self.store[self.front] + self.front = (self.front + 1) % INITIAL_QUEUE_SIZE + self.size -=1 + return tmp 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.size == 0: + return None + return self.store[self.front] - def size(self): """ Returns the number of elements in The Queue """ - pass - + return self.size + # self if a reference to the current class when in a class method you need to append self to it. def empty(self): """ Returns True if the Queue is empty And False otherwise. """ - pass + if self.size == 0: + return True + return False + 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. + """ - pass + tmp = [] + if(self.front == -1): + print ("Queue is Empty") + + elif (self.rear >= self.front): + for i in range(self.front, self.rear + 1): + tmp.append(self.store[i]) + + + else: + for i in range(self.front, INITIAL_QUEUE_SIZE): + tmp.append(self.store[i]) + for i in range(0, self.rear + 1): + tmp.append(self.store[i]) + + + if ((self.rear + 1) % INITIAL_QUEUE_SIZE == self.front): + print("Queue is Full") + + return str(tmp) + + + + + \ No newline at end of file diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 94fb2a6..1940bb9 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -1,7 +1,27 @@ from stacks_queues.linked_list import LinkedList class StackEmptyException(Exception): - pass + ''' + https://towardsdatascience.com/how-to-define-custom-exception-classes-in-python-bfa346629bca + ''' + def __init__(self, *args): + if args: + self.message = args[0] + else: + self.message = None + + def __str__(self): + print('calling str') + if self.message: + return 'StackEmptyException, {0}'.format(self.message) + else: + return 'StackEmptyException has been raised' + ''' + raise StackEmptyException # MyCustom Error is being raised without any arguments, so None will be sent to the message attribute in the object. The str method will be called and will print the message 'MyCustomEror message has been raised' + raise StackEmptyException('We have a problem') + ''' + + class Stack: @@ -12,7 +32,8 @@ 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 +42,23 @@ def pop(self): The Stack is empty. returns None """ - pass + try: + return self.store.remove_first() + + except LinkedList.EmptyListError(): + raise StackEmptyException("The Stack is empty") + + + + def empty(self): """ Returns True if the Stack is empty And False otherwise """ - pass + if self.store.empty(): + return True + return False def __str__(self): """ Returns the Stack in String form like: