diff --git a/python/linked_list.py b/python/linked_list.py index 7692833..51055f5 100644 --- a/python/linked_list.py +++ b/python/linked_list.py @@ -1,19 +1,68 @@ -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 +class LinkList(): + def __init__(self, head=None): + self.head = head + self.length = 1 + print(self.head) + # we're going to store the length of the Linked List here def add(self, data): # write your code to ADD an element to the Linked List - pass + self.length += 1 + #for node in range(self.length-1) + node = self.head + while node.next != None: + node = node.next + node.next = data def remove(self, data): - # write your code to REMOVE an element from the Linked List - pass + self.length -=1 + node = self.head + if node == data: + self.head = node.next + else: + while data != node: + previous = node + node = node.next + if node.next == None: + previous.next = None + else: + previous.next = node.next + + def get(self, element): + node = self.head + for index in range(self.length-1): + if node == element: + return index + else: + node = node.next - def get(self, element_to_get): - # write you code to GET and return an element from the Linked List - pass # ----- Node ------ class Node: - # store your DATA and NEXT values here - pass + def __init__(self, value): + self.value = value + self.next = None + +a = Node('a') +b = Node('b') +c = Node('c') +d = Node('d') + +test = LinkList(a) +test.add(b) +test.add(c) +test.add(d) + + +print(test.head.value) +print(test.head.next.value) +print(test.head.next.next.value) +print(test.head.next.next.next.value) + +print(test.get(c)) +print('-'*25) + +# print(test.head.value) +# print(test.head.next.value) +# print(test.head.next.next.value) +# print(test.head.next.next.next.value) \ No newline at end of file diff --git a/python/queue.py b/python/queue.py index f52c1a1..1015cf7 100644 --- a/python/queue.py +++ b/python/queue.py @@ -1,14 +1,31 @@ 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.length = 0 + 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 enqueue(self,data): + self.queue.append(data) + self.length += 1 + + def dequeue(self): + self.queue.pop(0) + self.length -= 1 def size(self): - # write your code that returns the size of the Queue - pass + return self.length + +q = Queue() +a = 'a' +b = 'b' +c = 'c' +d = 'd' + +print(q.queue) +q.enqueue(a) +q.enqueue(b) +q.enqueue(c) +q.enqueue(d) +print(q.queue, q.length) +q.dequeue() +print(q.queue, q.length) \ No newline at end of file diff --git a/python/stack.py b/python/stack.py index dd102b1..8b22cff 100644 --- a/python/stack.py +++ b/python/stack.py @@ -1,14 +1,33 @@ class Stack: + def __init__(self): + self.length = 0 + self.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 push(self, data): # write your code to add data following LIFO and return the Stack - pass - - def pop(self, data): + self.stack = [data] + self.stack + self.length += 1 + def pop(self): # write your code to removes the data following LIFO and return the Stack - pass + self.stack.pop(0) + self.length -= 1 def size(self): - # write your code that returns the size of the Stack - pass + return self.length + +s = Stack() +a = 'a' +b = 'b' +c = 'c' +d = 'd' + +print(s.stack) +s.push(a) +s.push(b) +s.push(c) +s.push(d) +print(s.stack, s.length) +s.pop() +print(s.stack, s.length) \ No newline at end of file