diff --git a/python/linked_list.py b/python/linked_list.py index 7692833..ac6cae0 100644 --- a/python/linked_list.py +++ b/python/linked_list.py @@ -1,19 +1,62 @@ 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 + self.length = 0 def add(self, data): # write your code to ADD an element to the Linked List - pass + new_node = Node(data) + old_node = self.head + self.head = new_node + self.head.next = old_node + + self.length += 1 + def remove(self, data): # write your code to REMOVE an element from the Linked List - pass + if self.head is None: + return False + if self.head == data: + head = self.head.next + else: + current = self.head + while current.next: + if current.next.data == data: + current.next = current.next.next + self.length -= 1 + return current.next + current = current.next + def get(self, element_to_get): # write you code to GET and return an element from the Linked List - pass + if self.head is None: + return False + else: + current = self.head + if current.data == element_to_get: + return current.data + while current.next: + current = current.next + if current.data == element_to_get: + return current.data + return False # ----- Node ------ class Node: # store your DATA and NEXT values here - pass + def __init__(self, data): + self.data = data + self.next = None + + +test = LinkList() +test.add(1) +test.add(2) +test.add(3) +print(test.length) + +test.remove(1) +print(test.length) diff --git a/python/queue.py b/python/queue.py index f52c1a1..c357155 100644 --- a/python/queue.py +++ b/python/queue.py @@ -1,14 +1,30 @@ 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.element_total = 0 + self.element_queue = [] - def enqueue(self): + def enqueue(self, data): # write your code to add data to the Queue following FIFO and return the Queue - pass + self.element_queue.insert(0, data) + self.element_total += 1 - def dequeue(self, data): + def dequeue(self): # write your code to removes the data to the Queue following FIFO and return the Queue - pass + self.element_queue.pop() + self.element_total -= 1 def size(self): # write your code that returns the size of the Queue - pass + return self.element_total + +test = Queue() + +test.enqueue(1) +test.enqueue(2) +test.enqueue(3) +print(test.element_queue) +print(test.element_total) +test.dequeue() +print(test.element_queue) +print(test.element_total) \ No newline at end of file diff --git a/python/stack.py b/python/stack.py index dd102b1..d62267e 100644 --- a/python/stack.py +++ b/python/stack.py @@ -1,14 +1,31 @@ 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.element_total = 0 + self.element_stack = [] - def push(self): + def push(self, element): # write your code to add data following LIFO and return the Stack - pass + self.element_stack.append(element) + self.element_total += 1 - def pop(self, data): + def pop(self): # write your code to removes the data following LIFO and return the Stack - pass + self.element_stack.pop() + self.element_total -= 1 def size(self): # write your code that returns the size of the Stack - pass + return self.element_total + + +test = Stack() + +print(test.size()) +test.push(1) +test.push(2) +print(test.element_stack) +print(test.size()) +test.pop() +print(test.element_stack) +print(test.size()) \ No newline at end of file