Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions stacks_queues/queue.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from stacks_queues.linked_list import LinkedList

INITIAL_QUEUE_SIZE = 20

Expand All @@ -15,47 +16,82 @@ def __init__(self):
self.front = -1
self.rear = -1
self.size = 0


def enqueue(self, element):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" Adds an element to the Queue
Raises a QueueFullException if all elements
In the store are occupied
returns None
"""
pass
if self.front == -1 & self.rear == -1:
self.front = 0
self.rear = 0
elif self.rear == self.front:
raise QueueFullException(f"Queue is full.")

self.store[self.rear] = element
self.rear = (self.rear + 1) % self.buffer_size
self.size += 1

def dequeue(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice work! Just have one suggestion below ⬇️

""" Removes and returns an element from the Queue
Raises a QueueEmptyException if
The Queue is empty.
"""
pass
if self.rear == -1 and self.front == -1:
raise QueueEmptyException("Queue is empty.")

if self.size == 0:
self.front = -1
self.rear = -1
Comment on lines +44 to +46

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend moving this to after line 52. If the queue is empty to start, it should be caught by your if statement on line 41. However, it may be the case that after dequeueing an element, your queue is now empty and you want to reset the front and rear pointers.


front_item = self.store[self.front]
self.store[self.front] = None

self.front = (self.front + 1) % self.buffer_size
self.size -= 1

return front_item

def front(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" 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:
raise QueueEmptyException("Queue is empty")
return self.store[self.front]


def size(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" Returns the number of elements in
The Queue
"""
pass
return self.size

def empty(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" Returns True if the Queue is empty
And False otherwise.
"""
pass
return self.size == 0

def __str__(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" 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
list = []
current_item = self.front
count = 0

if self.size == 0:
return str(list)
while count < self.size:
list.append((str(self.store[current_item])).replace("'", ""))
current_item =(current_item+1) % self.buffer_size
count +=1
return str(list).replace("'", "")



31 changes: 10 additions & 21 deletions stacks_queues/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,19 @@ def __init__(self):
self.store = LinkedList()

def push(self, element):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" Adds an element to the top of the Stack.
Returns None
"""
pass
self.store.add_first(element)
return None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functions return None by default. If we aren't returning any other values, we usually prefer to let the function to implicitly return None

Suggested change
return None


def pop(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" Removes an element from the top
Of the Stack
Raises a StackEmptyException if
The Stack is empty.
returns None
"""
pass
if self.store.empty():
raise StackEmptyException("Stack is empty.")
return self.store.remove_first()

def empty(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" Returns True if the Stack is empty
And False otherwise
"""
pass
return self.store.empty()

def __str__(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" 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
if self:
return str(self.store)
return ""