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
28 changes: 14 additions & 14 deletions stacks_queues/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ def remove_first(self):
self.head = self.head.next
if self.head:
self.head.previous = None

return value


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 All @@ -57,7 +57,7 @@ def empty(self):
def search(self, value):
if not self.head:
return False

if self.head.value == value:
return True

Expand All @@ -66,7 +66,7 @@ def search(self, value):
if current.value == value:
return True
current = current.next

return False


Expand All @@ -77,7 +77,7 @@ def search(self, value):
def find_max(self):
if not self.head:
return None

current = self.head
max = current.value

Expand All @@ -102,7 +102,7 @@ def find_min(self):
if min > current.value:
min = current.value
current = current.next

return min

# method that returns the length of the singly linked list
Expand All @@ -111,13 +111,13 @@ def find_min(self):
def length(self):
if not self.head:
return 0

current = self.head
length = 0
while current:
length += 1
current = current.next

return length

# method that returns the value at a given index in the linked list
Expand All @@ -136,7 +136,7 @@ def get_at_index(self, index):
return current.value
count += 1
current = current.next

return None

# method to print all the values in the linked list
Expand All @@ -149,7 +149,7 @@ def visit(self):
while current:
values.append(current.value)
current = current.next

print(", ".join(values))

# method to delete the first node found with specified value
Expand All @@ -158,14 +158,14 @@ def visit(self):
def delete(self, value):
if not self.head:
return None

current = self.head
if current.value == value:
self.head = current.next
if self.head:
self.head.previous = None
else:
tail = None
self.tail = None
return None

previous = current
Expand Down Expand Up @@ -210,7 +210,7 @@ def add_last(self, value):
if not self.head:
self.add_first(value)
return

self.tail.next = new_node
new_node.previous = self.tail
self.tail = new_node
Expand All @@ -229,7 +229,7 @@ def remove_last(self):
return value

# method that returns the value of the last node in the linked list
# returns nil if the linked list is empty
# returns None if the linked list is empty
# Time Complexity: O(n) where n is the number of nodes
# Space Complexity: O(1)
def get_last(self):
Expand Down
46 changes: 28 additions & 18 deletions stacks_queues/queue.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

INITIAL_QUEUE_SIZE = 20

class QueueFullException(Exception):
Expand All @@ -12,50 +11,61 @@ class Queue:
def __init__(self):
self.store = [None] * INITIAL_QUEUE_SIZE
self.buffer_size = INITIAL_QUEUE_SIZE
self.front = -1
self.rear = -1
self.front = 0
self.rear = 0
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.size == self.buffer_size:
raise QueueFullException("i'm full 🤰")
else:
self.size += 1
self.store[self.rear] = element
self.rear = (self.rear + 1) % self.buffer_size


def dequeue(self):

Choose a reason for hiding this comment

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

👍

""" Removes and returns an element from the Queue
Raises a QueueEmptyException if
Raises a QueueEmptyException if
The Queue is empty.
"""
pass

def front(self):
if self.empty():
raise QueueEmptyException("i'm empty")
else:
temp = self._front()
self.size -= 1
self.front = (self.front + 1) % self.buffer_size
return temp

def _front(self):
""" Returns an element from the front
of the Queue and None if the Queue
is empty. Does not remove anything.
"""
pass


def size(self):
""" Returns the number of elements in
The Queue
"""
pass
return self.store[self.front] if not self.empty() else None

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):
""" 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
f = self.front
helper_list = []

for i in range(self.size):
helper_list.append(self.store[(f+i) % self.buffer_size])

return "["+", ".join(list(map(str, helper_list)))+"]"
14 changes: 10 additions & 4 deletions stacks_queues/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def push(self, element):
""" Adds an element to the top of the Stack.

Choose a reason for hiding this comment

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

Stack looks good!

Returns None
"""
pass

return self.store.add_first(element)

def pop(self):
""" Removes an element from the top
Expand All @@ -21,18 +22,23 @@ def pop(self):
The Stack is empty.
returns None
"""
pass
if not self.empty():
return self.store.remove_first()
else:
raise StackEmptyException("error")


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


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)