diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..d41508f78 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,52 @@ +#Time Complexity: +# push: O(1) +# pop: O(1) +# peek: O(1) +# isEmpty(): O(1) +# size: O(1) +# show: O(n) +#Space Complexity: for all operations O(n) where n is the number of elements +#Yes the code ran on Leetcode + 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 size(self): - - def show(self): - + def __init__(self): + """Initialize an empty stack using a Python list""" + self.stack = [] + + + def isEmpty(self): + """If the stack is empty return True""" + return len(self.stack) == 0 + + def push(self, item): + """Add an element to the top of the stack""" + self.stack.append(item) + + def pop(self): + """Remove an element from top of element and return the top element + and return None if the stack is empty""" + if self.isEmpty()==True: + return None + return self.stack.pop() + -s = myStack() -s.push('1') -s.push('2') -print(s.pop()) -print(s.show()) + def peek(self): + """Return the top element of the stack without removing any element""" + return self.stack[-1] + + def size(self): + """Return the length of the stack""" + return len(self.stack) + + def show(self): + """Return the stack elements as a list (bottom → top)""" + return self.stack + +if __name__ == "__main__": + s = myStack() + s.push('1') + s.push('2') + print(s.pop()) + print(s.show()) diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..1bc9a50bb 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,15 +1,42 @@ +#Time Complexity: +# push: O(1) +# pop: O(1) +# peek: O(1) +# isEmpty(): O(1) +# size: O(1) +# show: O(n) +#Space Complexity: for all operations O(n) where n is the number of elements +#Yes the code ran on Leetcode + class Node: + """Node class for Linked List""" def __init__(self, data): self.data = data self.next = None class Stack: def __init__(self): + """Initialize an empty stack using a linked list""" + self.head = None + self._size = 0 def push(self, data): + """Insert a new element on top of stack using linked list""" + new_node = Node(data) + new_node.next = self.head + self.head = new_node + self._size +=1 def pop(self): + """Return the popped element from stack and return none if its empty""" + if self._size==0: + return None + popped = self.head.data + self.head = self.head.next + self._size -= 1 + return popped + a_stack = Stack() while True: diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..77c236185 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,6 +3,8 @@ class ListNode: A node in a singly-linked list. """ def __init__(self, data=None, next=None): + self.data = data + self.next = None class SinglyLinkedList: def __init__(self): @@ -17,16 +19,80 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ - + new_node = ListNode(data) + if not self.head: #if list is empty + self.head = new_node + return self.head + current = self.head + while current.next: + current = current.next + current.next = new_node + def find(self, key): """ Search for the first element with `data` matching `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. """ + current = self.head + prev = None + + #if key is head then remove it + if current and current.data == key: + self.head = current.next + current = None + return + + #Traverse the list + while current and current.data != key: + prev = current + current = current.next + + # If key not found + if current is None: + return + + # Unlink the node + prev.next = current.next + current = None + + def display(self): + """ + Return all elements of the list as a Python list. + Takes O(n) time. + """ + elements = [] + current = self.head + while current: + elements.append(current.data) + current = current.next + return elements + + +ll = SinglyLinkedList() +ll.append(10) +ll.append(20) +ll.append(30) +print("List:", ll.display()) # [10, 20, 30] + +node = ll.find(20) +print("Found:", node.data if node else None) # 20 + +ll.remove(20) +print("After removing 20:", ll.display()) # [10, 30] + +ll.remove(100) # Key not in list +print("After trying to remove 100:", ll.display()) # [10, 30] +