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
32 changes: 26 additions & 6 deletions stacks_queues/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,65 @@ def __init__(self):
self.size = 0


# methods to add to the Queue class
def enqueue(self, element):
""" Adds an element to the Queue
Raises a QueueFullException if all elements
In the store are occupied
returns None
"""
pass

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

resubmitted, some formatting was off for questions, and missing a couple answers

if self.size == self.buffer_size:
raise QueueFullException
self.store[self.rear] = element
self.rear = (self.rear + 1) % self.buffer_size
self.size += 1


def dequeue(self):
""" Removes and returns an element from the Queue
Raises a QueueEmptyException if
The Queue is empty.
"""
pass

Choose a reason for hiding this comment

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

if self.size == 0:
raise QueueEmptyException
element = self.store[self.front]
self.front = (self.front + 1) % self.buffer_size
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.front == -1:
return None
Comment on lines +53 to +54

Choose a reason for hiding this comment

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

Because you never check in dequeue whether dequeuing an element makes the Queue empty and, if the Queue does become empty, reset front to point at index -1, it may not always be true that the front pointer is at index -1 when the queue is empty. Try using the empty method you implement instead.

Suggested change
if self.front == -1:
return None
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

Choose a reason for hiding this comment

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


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

Choose a reason for hiding this comment

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


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.

Choose a reason for hiding this comment

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

"""
pass
values = []
index = self.front
for i in range(0, self.size):
values.append(str(self.store[index]))
index = (index + 1) % self.buffer_size
return "[" + ", ".join(values) + "]"
13 changes: 9 additions & 4 deletions stacks_queues/stack.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from tokenize import String
from stacks_queues.linked_list import LinkedList

class StackEmptyException(Exception):
Expand All @@ -7,12 +8,15 @@ class Stack:

def __init__(self):
self.store = LinkedList()
self.front = -1
self.rear = -1
self.size = 0

def push(self, element):
""" Adds an element to the top of the Stack.
Returns None
"""
pass
self.store.add_first(element)

Choose a reason for hiding this comment

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


def pop(self):
""" Removes an element from the top
Expand All @@ -21,18 +25,19 @@ def pop(self):
The Stack is empty.
returns None
"""
pass
return self.store.remove_first()

Choose a reason for hiding this comment

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


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

Choose a reason for hiding this comment

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


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.
"""
pass

return str(self.store)