From 3d5968b0aab431d7de6bc76fe92412d37e71cde3 Mon Sep 17 00:00:00 2001 From: danielwhaleman <“[whaleman@gmail.com]”> Date: Thu, 21 May 2020 20:44:45 -0500 Subject: [PATCH] Dan Whaley and Eric Murphy Solution --- python/linked_list.py | 53 +++++++++++++++++++++++++++++++++++-------- python/queue.py | 23 +++++++++++-------- python/stack.py | 23 +++++++++++-------- runner.py | 37 ++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 29 deletions(-) create mode 100644 runner.py diff --git a/python/linked_list.py b/python/linked_list.py index 7692833..5a97a11 100644 --- a/python/linked_list.py +++ b/python/linked_list.py @@ -1,19 +1,54 @@ 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 = None + + def __str__(self): + return_list = [] + current = self.head + while current: + return_list.append(current.data) + current = current.next + return ', '.join(str(data) for data in return_list) def add(self, data): - # write your code to ADD an element to the Linked List - pass + insert = Node(data) + if self.head: + current = self.head + while current.next: + current = current.next + current.next = insert + else: + self.head = insert def remove(self, data): - # write your code to REMOVE an element from the Linked List - pass + previous = None + if self.head.data == data: + self.head.next.data = self.head.data + self.head = self.head.next + current = self.head + while True: + if current.data == data: + previous.next = current.next + break + else: + previous = current + current = current.next + def get(self, element_to_get): - # write you code to GET and return an element from the Linked List - pass + current = self.head + if current.data == element_to_get: + return current + while True: + if current.data == element_to_get: + return current + else: + current = current.next # ----- Node ------ class Node: - # store your DATA and NEXT values here - pass + def __init__(self, data=None, next=None): + self.data = data + self.next = next + diff --git a/python/queue.py b/python/queue.py index f52c1a1..b89868f 100644 --- a/python/queue.py +++ b/python/queue.py @@ -1,14 +1,17 @@ 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): + self.queue = [] + self.total = len(self.queue) + + def enqueue(self, value): + self.queue.insert(0, value) + self.total = len(self.queue) - def enqueue(self): - # write your code to add data to the Queue following FIFO and return the Queue - pass - - def dequeue(self, data): - # write your code to removes the data to the Queue following FIFO and return the Queue - pass + def dequeue(self): + self.queue.pop(-1) 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..d0479f8 100644 --- a/python/stack.py +++ b/python/stack.py @@ -1,14 +1,17 @@ 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): + self.stack = [] + self.total = len(self.stack) + + def push(self, data): + self.stack.append(data) - def push(self): - # write your code to add 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 - pass + def pop(self): + self.stack.pop() def size(self): - # write your code that returns the size of the Stack - pass + return self.total + + + + diff --git a/runner.py b/runner.py new file mode 100644 index 0000000..3180764 --- /dev/null +++ b/runner.py @@ -0,0 +1,37 @@ +from python.linked_list import LinkList +from python.queue import Queue +from python.stack import Stack + +linked_list = LinkList() + +linked_list.add(6) +linked_list.add(9) +linked_list.add(11) +linked_list.add(4) +linked_list.add(7) + +print(linked_list) + +linked_list.get(9) +linked_list.remove(4) +print(linked_list) + +queue = Queue() + +queue.enqueue(0) +queue.enqueue(1) +queue.enqueue(2) +queue.enqueue(3) +print(queue.queue) +# should be 0 +queue.dequeue() +print(queue.queue) + +stack = Stack() +stack.push(0) +stack.push(1) +stack.push(2) +stack.push(3) +print(stack.stack) +stack.pop() +print(stack.stack) \ No newline at end of file