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
2 changes: 1 addition & 1 deletion stacks_queues/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 31 additions & 7 deletions stacks_queues/queue.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

from logging import raiseExceptions


INITIAL_QUEUE_SIZE = 20

class QueueFullException(Exception):
Expand All @@ -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):
Expand All @@ -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
"""
Comment on lines 27 to -25

Choose a reason for hiding this comment

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

⚠️ This doesn't work as a circular buffer. I suggest using the self.front and self.rear indexes.

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.
"""
Comment on lines 41 to 45

Choose a reason for hiding this comment

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

⚠️ Ditto with this method

pass
result = self.store.pop(0)
if result is None:
return self.dequeue()
return result

def front(self):
""" Returns an element from the front
Expand All @@ -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
Expand All @@ -50,12 +73,13 @@ 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:
[3, 4, 7]
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])

6 changes: 3 additions & 3 deletions stacks_queues/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down