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
93 changes: 65 additions & 28 deletions stacks_queues/queue.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,98 @@

INITIAL_QUEUE_SIZE = 20


class QueueFullException(Exception):
pass


class QueueEmptyException(Exception):
pass

class Queue:

class Queue:
def __init__(self):
self.store = [None] * INITIAL_QUEUE_SIZE
self.buffer_size = INITIAL_QUEUE_SIZE
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
"""Adds an element to the Queue
Raises a QueueFullException if all elements
In the store are occupied
returns None
"""
pass
if self.size == len(self.store):

Choose a reason for hiding this comment

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

raise QueueFullException

if self.front == -1:
self.front = 0
self.rear = 0
self.store[self.rear] = element

elif self.rear == len(self.store) - 1:
self.rear = 0
self.store[self.rear] = element
else:
self.rear += 1
self.store[self.rear] = element
self.size += 1

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

Choose a reason for hiding this comment

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

raise QueueEmptyException

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

if self.front == self.rear:
self.front = -1
self.rear = -1

elif self.front == len(self.store) - 1:
self.front = 0
else:
self.front += 1
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.
"""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:

Choose a reason for hiding this comment

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

return self.store[self.front]

def size(self):
""" Returns the number of elements in
The Queue
"""Returns the number of elements in
The Queue
"""
pass
return self.size

def empty(self):
""" Returns True if the Queue is empty
And False otherwise.
"""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.
"""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
q = []

Choose a reason for hiding this comment

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

start = self.front
while len(q) < self.size:
q.append(self.store[start])
if start < len(self.store) - 1:
start += 1
else:
start = 0
return str(q)
37 changes: 19 additions & 18 deletions stacks_queues/stack.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
from stacks_queues.linked_list import LinkedList


class StackEmptyException(Exception):
pass

class Stack:

class Stack:
def __init__(self):
self.store = LinkedList()

def push(self, element):
""" Adds an element to the top of the Stack.
Returns None
"""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
Of the Stack
Raises a StackEmptyException if
The Stack is empty.
returns None
"""Removes an element from the top
Of the Stack
Raises a StackEmptyException if
The Stack is empty.
returns None
"""
pass
return self.store.remove_first() if not self.empty() else StackEmptyException()

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
"""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.
"""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 self.store.visit()

Choose a reason for hiding this comment

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