diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/python/linked_list.py b/python/linked_list.py index 7692833..db28329 100644 --- a/python/linked_list.py +++ b/python/linked_list.py @@ -1,19 +1,67 @@ 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): - # write your code to ADD an element to the Linked List - pass + if not self.head: + new_node = Node(data) + self.head = new_node + else: + previous_node = self.head + while previous_node.next_node: + current_node = previous_node.next_node + previous_node = current_node + new_node = Node(data) + previous_node.next_node = new_node + self.length += 1 + def remove(self, data): - # write your code to REMOVE an element from the Linked List - pass + if self.head.data == data: + self.head = self.head.next_node + else: + previous_node = self.head + current_node = self.head.next_node + while current_node.data != data: + previous_node = current_node + current_node = current_node.next_node + previous_node.next_node = current_node.next_node + self.length -= 1 + def get(self, element_to_get): - # write you code to GET and return an element from the Linked List - pass + current_node = self.head + for _i in range(0, element_to_get): + current_node = current_node.next_node + return current_node.data + # ----- Node ------ class Node: - # store your DATA and NEXT values here - pass + + def __init__(self, data, next_node=None): + self.data = data + self.next_node=next_node + + +ll = LinkList() +ll.add(1) +ll.add(2) +ll.add(3) + +print(ll.head) +print(ll.head.data) +print(ll.head.next_node.data) +print(ll.head.next_node.next_node.data) +print(ll.length) + +# ll.remove(3) + +print(ll.head.data) +print(ll.head.next_node.data) +print(ll.length) + +print(ll.get(0)) + diff --git a/python/queue.py b/python/queue.py index f52c1a1..0177482 100644 --- a/python/queue.py +++ b/python/queue.py @@ -1,14 +1,23 @@ 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, data): # write your code to add data to the Queue following FIFO and return the Queue - pass + self.queue.append(data) + self.total += 1 + return self.queue - def dequeue(self, data): + def dequeue(self): # write your code to removes the data to the Queue following FIFO and return the Queue - pass + if not self.queue: + return 'Nothing to dequeue' + self.queue.pop(0) + self.total -= 1 + return self.queue 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..f9e6405 100644 --- a/python/stack.py +++ b/python/stack.py @@ -1,14 +1,23 @@ 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.total = 0 + self.stack = [] - def push(self): + def push(self, data): # write your code to add data following LIFO and return the Stack - pass + self.stack.append(data) + self.total += 1 + return self.stack - def pop(self, data): + def pop(self): # write your code to removes the data following LIFO and return the Stack - pass + if not self.stack: + return 'Nothing to pop' + 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