From 31578d2b9a78005e29719c2aba16e61645b1a89f Mon Sep 17 00:00:00 2001 From: D-Lindberg <58619523+D-Lindberg@users.noreply.github.com> Date: Fri, 22 May 2020 08:32:48 -0500 Subject: [PATCH] completed with Coby Leuschke --- python/linked_list.py | 41 ++++++++++++++++++++++++++++++++++++----- python/queue.py | 22 ++++++++++++++++------ python/stack.py | 21 ++++++++++++++++----- 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/python/linked_list.py b/python/linked_list.py index 7692833..4be7ebb 100644 --- a/python/linked_list.py +++ b/python/linked_list.py @@ -1,19 +1,50 @@ 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 + def __init__(self): + self.head = Node('HEAD') + self.size = 0 def add(self, data): # write your code to ADD an element to the Linked List - pass + new_node = Node(data) + #find the starting point + current_position = self.head - def remove(self, data): + #find our way to the end + while current_position.next: + previous = current_position + current_position = previous.next + + #now at the end lets make sure the previous node knows we are here + current_position.next = new_node + self.size += 1 + + def remove(self, target_data): # write your code to REMOVE an element from the Linked List - pass + current_position = self.head + while current_position.data != target_data: + previous = current_position + current_position = previous.next + previous.next = current_position.next + self.size -1 def get(self, element_to_get): # write you code to GET and return an element from the Linked List - pass + if element_to_get <= self.size: + current_position = self.head + current_element = 0 + while current_element < element_to_get: + previous = current_position + current_position = previous.next + current_element +=1 + return current_position.data + # ----- Node ------ class Node: # store your DATA and NEXT values here - pass + def __init__(self, data): + self.data = data + self.next = None + + diff --git a/python/queue.py b/python/queue.py index f52c1a1..c597500 100644 --- a/python/queue.py +++ b/python/queue.py @@ -1,14 +1,24 @@ 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 + def __init__(self): + # every time we add to queue need to increment a counter + self.queue = [] + self.total = 0 - def enqueue(self): + def enqueue(self, data): # write your code to add data to the Queue following FIFO and return the Queue - pass + self.queue.append(data) + self.total +=1 + return self.queue + + def dequeue(self): + # write your code to removes the data to the Queue following FIFO and return the first item + if self.total > 0: + value = self.queue.pop(0) + self.total -= 1 + return value - def dequeue(self, data): - # write your code to removes the data to the Queue following FIFO and return the Queue - pass def size(self): # write your code that returns the size of the Queue - pass + return self.total diff --git a/python/stack.py b/python/stack.py index dd102b1..e04443b 100644 --- a/python/stack.py +++ b/python/stack.py @@ -1,14 +1,25 @@ 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 + def __init__(self): - def push(self): + self.stack = [] + self.total = 0 + + + def push(self, data): # write your code to add data following LIFO and return the Stack - pass + self.stack.append(data) + self.total += 1 + return self.stack - def pop(self, data): + def pop(self): # write your code to removes the data following LIFO and return the Stack - pass + if self.total > 0: + value = self.stack.pop() + self.total -= 1 + return value + def size(self): # write your code that returns the size of the Stack - pass + return self.total