diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..99eaa59c7 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,20 +1,33 @@ class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file - def __init__(self): - - def isEmpty(self): - - def push(self, item): - - def pop(self): - - - def peek(self): + def __init__(self): + self.stack = [] + + def isEmpty(self): + return len(self.stack) == 0 + + def push(self, item): + self.stack.append(item) + + def pop(self): + if self.isEmpty(): + return "Stack is empty" + else: + return self.stack.pop() + + def peek(self): + if self.isEmpty(): + return "Stack is empty" + else: + return self.stack[-1] + - def size(self): + def size(self): + return len(self.stack) - def show(self): + def show(self): + return self.stack s = myStack() @@ -22,3 +35,15 @@ def show(self): s.push('2') print(s.pop()) print(s.show()) + +''' +Time Complexity : O(1) +push() : O(1) +pop() : O(1) +peek() : O(1) +isEmpty() : O(1) +size() : O(1) +show() : O(1) + +Space Completxity : O(n) +''' \ No newline at end of file diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..9cba25c50 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -6,10 +6,20 @@ def __init__(self, data): class Stack: def __init__(self): + self.top = None def push(self, data): + new_node = Node(data) + new_node.next = self.top + self.top = new_node def pop(self): + if self.top is None: + return None + + popped_node = self.top + self.top = self.top.next + return popped_node.data a_stack = Stack() while True: @@ -30,3 +40,12 @@ def pop(self): print('Popped value: ', int(popped)) elif operation == 'quit': break + +''' +Time Complexity : O(1) +Push : O(1) +Pop : O(1) +Peek : O(1) + +Space Complexity : O(n) +''' \ No newline at end of file diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..4175897ce 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,6 +3,9 @@ class ListNode: A node in a singly-linked list. """ def __init__(self, data=None, next=None): + self.data = data + self.next = next + class SinglyLinkedList: def __init__(self): @@ -17,6 +20,16 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ + new_node = ListNode(data) + + if self.head is None: + self.head = new_node + return + + current = self.head + while current.next: + current = current.next + current.next = new_node def find(self, key): """ @@ -24,9 +37,44 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + current = self.head + + while current: + if current.data == key: + return current + current = current.next + + return None + def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + if self.head is None: + return + + if self.head.data == key: + self.head = self.head.next + return + + prev = self.head + current = self.head.next + + while current: + if current.data == key: + prev.next = current.next + return + prev = current + current = current.next + +''' +Time Complexity : O(n) +append : O(n) +find : O(n) +remove : O(n) + +Space Complexity : O(n) + +'''