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
72 changes: 60 additions & 12 deletions python/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,67 @@
class LinkList:
# write your __init__ method here that should store a 'head' value which the first Node in the LinkedList and a 'length' value which is the total number of Nodes in the LinkedList
# write your __init__ method here that should store a 'head' value which the first Node in the LinkedList and a 'length' value which is the total number of Nodes in the LinkedList
def __init__(self):
self.length = 0
self.head = Node(None)

def add(self, data):
# write your code to ADD an element to the Linked List
pass
def add(self, data):
# write your code to ADD an element to the Linked List
new_node = Node(data)
temp_ref = self.head
while temp_ref.next:
temp_ref = temp_ref.next
temp_ref.next = new_node
self.length += 1

def remove(self, data):
# write your code to REMOVE an element from the Linked List
pass
# Works except for last Node (bug)
def remove(self, data):
# write your code to REMOVE an element from the Linked List
temp_before = self.head
curr_node = self.head
while curr_node.next:
temp_next = curr_node.next
if data == curr_node.data:
temp_before.next = temp_next
del curr_node
self.length -= 1
return
else:
temp_before = curr_node
curr_node = curr_node.next
# Probably not the best way
# First Node edge case
if self.head.next.data == data:
temp_node = self.head.next
self.head.next = self.head.next.next
self.length -= 1
del(temp_node)

def get(self, element_to_get):
# write you code to GET and return an element from the Linked List
pass
def get(self, element_to_get):
# write you code to GET and return an element from the Linked List
pass

def print_nodes(self):
curr_in = 0
curr_node = self.head.next
while curr_in < self.length:
print(f"index: {curr_in} data: {curr_node.data}")
curr_in += 1
curr_node = curr_node.next
# ----- Node ------


class Node:
# store your DATA and NEXT values here
pass
# store your DATA and NEXT values here
def __init__(self, data):
self.data = data
self.next = None


test_link = LinkList()
test_link.add(5)
test_link.add(7)
test_link.add(3)
test_link.add(1)
test_link.remove(5)
test_link.remove(1)
test_link.print_nodes()
28 changes: 18 additions & 10 deletions python/queue.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
class Queue:
# write your __init__ method here that should store a 'total' value which is the total number of elements in the Queue and a 'queue' value which is an array of stored values in the Queue
# write your __init__ method here that should store a 'total' value which is the total number of elements in the Queue and a 'queue' value which is an array of stored values in the Queue
def __init__(self):
self.total = 0
self.queue = []

def enqueue(self):
# write your code to add data to the Queue following FIFO and return the Queue
pass
def enqueue(self, data):
# write your code to add data to the Queue following FIFO and return the Queue
self.queue.insert(0, data)
self.total += 1

def dequeue(self, data):
# write your code to removes the data to the Queue following FIFO and return the Queue
pass
def dequeue(self):
# write your code to removes the data to the Queue following FIFO and return the Queue
if len(self.queue > 0):
self.queue.pop()
self.total -= 1
else:
return -1

def size(self):
# write your code that returns the size of the Queue
pass
def size(self):
# write your code that returns the size of the Queue
return self.total
28 changes: 18 additions & 10 deletions python/stack.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
class Stack:
# write your __init__ method here that should store a 'total' value which is the total number of elements in the Stack and a 'stack' value which is an array of stored values in the Stack
# write your __init__ method here that should store a 'total' value which is the total number of elements in the Stack and a 'stack' value which is an array of stored values in the Stack
def __init__(self):
self.storage = []

def push(self):
# write your code to add data following LIFO and return the Stack
pass
def push(self, data):
# write your code to add data following LIFO and return the Stack
self.storage.append(data)

def pop(self, data):
# write your code to removes the data following LIFO and return the Stack
pass
def pop(self, data):
# write your code to removes the data following LIFO and return the Stack
if len(self.storage) == 0:
return -1
self.storage.pop()

def size(self):
# write your code that returns the size of the Stack
pass
def size(self):
# write your code that returns the size of the Stack
return len(self.storage)


test_list = []
print(test_list.pop())