From 033497c086c1b6e36bc7eee33c78480a784cd4f6 Mon Sep 17 00:00:00 2001 From: Jon Carlton Date: Thu, 18 Feb 2021 19:04:22 -0500 Subject: [PATCH] Release 0 --- python/linked_list.py | 68 ++++++++++++++++++++++++++++++++++++++----- python/queue.py | 35 +++++++++++++++------- python/stack.py | 36 ++++++++++++++++------- 3 files changed, 111 insertions(+), 28 deletions(-) diff --git a/python/linked_list.py b/python/linked_list.py index 7692833..7e9617a 100644 --- a/python/linked_list.py +++ b/python/linked_list.py @@ -1,19 +1,71 @@ 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, head = None): + self.head = head + self.length = 0 - def add(self, data): + def prepend(self, data): # write your code to ADD an element to the Linked List - pass + new_head = Node(data) + old_head = self.head + self.head = new_head + self.head.next = old_head - def remove(self, data): + self.length += 1 + + def remove(self, target_value): # write your code to REMOVE an element from the Linked List - pass + temp = self.head + if temp.data == target_value: # first element is target data + self.head = temp.next + return + while temp.next: + prev = temp + temp = temp.next + if temp.data == target_value: + prev.next = temp.next + + self.length -= 1 - def get(self, element_to_get): + def get(self, index_to_get): # write you code to GET and return an element from the Linked List - pass + count = 0 + if index_to_get == count: + return self.head.data + else: + while self.head.next: + self.head = self.head.next + count += 1 + if index_to_get == count: + return self.head.data + + + def print_list(self): + current = self.head + while current: + print(current.data) + current = current.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 + +ll = LinkList() +ll.prepend(1) +ll.prepend(2) +ll.prepend(3) +ll.prepend(4) + +ll.print_list() +print('\n') +ll.remove(2) +ll.print_list() + +ll.prepend(5) +ll.prepend(6) +ll.print_list() +print('\nGet value at index 2: ') +print(ll.get(2)) \ No newline at end of file diff --git a/python/queue.py b/python/queue.py index f52c1a1..c1334cc 100644 --- a/python/queue.py +++ b/python/queue.py @@ -1,14 +1,29 @@ 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.num_elements = 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.num_elements += 1 + return self.queue - 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 + popped_item = self.queue.pop(0) + self.num_elements -= 1 + return popped_item - 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.num_elements + +# q = Queue() +# q.enqueue(1) +# q.enqueue(2) +# q.enqueue(3) +# print(q.size()) +# q.dequeue() +# print(q.size()) \ No newline at end of file diff --git a/python/stack.py b/python/stack.py index dd102b1..6d5ee54 100644 --- a/python/stack.py +++ b/python/stack.py @@ -1,14 +1,30 @@ 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.num_elements = 0 + 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) + self.num_elements += 1 - def pop(self, data): - # write your code to removes the data following LIFO and return the Stack - pass + def pop(self): + # write your code to removes the data following LIFO and return the Stack + popped_item = self.storage.pop() + self.num_elements -= 1 + return popped_item - 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 self.num_elements + +# s = Stack() +# s.push(1) +# s.push(2) +# s.push(3) +# s.push(4) +# print(s.storage) +# s.pop() +# print(s.storage) +# print(s.size()) \ No newline at end of file