diff --git a/python/linked_list.py b/python/linked_list.py index 7692833..c3e970f 100644 --- a/python/linked_list.py +++ b/python/linked_list.py @@ -1,19 +1,58 @@ 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 + # store your DATA and NEXT values here + def __init__(self, data): + self.data = data + self.next = None + + +l = LinkList() +l.add(9) +l.add(8) +l.add(7) +l.remove(1) +print(l.length) diff --git a/python/queue.py b/python/queue.py index f52c1a1..8efbb05 100644 --- a/python/queue.py +++ b/python/queue.py @@ -1,14 +1,26 @@ 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.total = 0 + self.queue = [] - def enqueue(self): + def enqueue(self, elem): # write your code to add data to the Queue following FIFO and return the Queue - pass + self.queue.insert(0, elem) + self.total += 1 def dequeue(self, data): # write your code to removes the data to the Queue following FIFO and return the Queue - pass + self.queue.pop() + self.total -= 1 def size(self): # write your code that returns the size of the Queue - pass + return self.total + +q = Queue() +q.enqueue(5) +q.enqueue(9) +q.enqueue(7) +q.dequeue(5) +print(q.size()) \ No newline at end of file diff --git a/python/stack.py b/python/stack.py index dd102b1..fb03946 100644 --- a/python/stack.py +++ b/python/stack.py @@ -1,14 +1,29 @@ 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 push(self): + def __init__(self): + self.total = 0 + self.stack = [] + + def push(self, elem): # write your code to add data following LIFO and return the Stack - pass + self.stack.append(elem) + self.total += 1 + return self.stack def pop(self, data): # write your code to removes the data following LIFO and return the Stack - pass + self.stack.pop() + self.total -= 1 + return self.stack def size(self): # write your code that returns the size of the Stack - pass + return self.total + + +s = Stack() +s.push(2) +s.pop(1) +s.push(2) +print(s.size())