diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..e1e05a96e 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,20 +1,65 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 8 16:20:59 2026 + +@author: rishigoswamy + +# Time Complexity: +# isEmpty : O(1) +# push : O(1) +# pop : O(1) +# peek : O(1) +# size : O(1) +# show : O(n) # iterates through stack + +# Space Complexity: +# O(n), where n is the number of elements in the stack (max 1000) + +""" + class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): + self.maxSize = 1000 + self.currentSize = 0 + self.stack = [] def isEmpty(self): + return not self.stack def push(self, item): + if self.currentSize < self.maxSize: + self.stack.append(item) + self.currentSize+=1 + else: + print("Stack Overflow") + def pop(self): + if self.stack: + self.currentSize-=1 + return self.stack.pop() + else: + print("Stack Underflow") + return 0 def peek(self): + if self.stack: + return self.stack[-1] + else: + print("Stack Underflow") + return 0 def size(self): + return len(self.stack) def show(self): + for item in self.stack: + print(item) + return self.stack s = myStack() diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..a280c7011 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,3 +1,18 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 8 16:20:59 2026 + +@author: rishigoswamy + +# Time Complexity: +# push : O(1) (insert at head) +# pop : O(1) (remove from head) +# +# Space Complexity: +# O(n), where n is the number of elements currently in the stack. + +""" class Node: def __init__(self, data): @@ -6,10 +21,23 @@ def __init__(self, data): class Stack: def __init__(self): + self.head = Node(None) def push(self, data): + newNode = Node(data) + if self.head.next: + newNode.next = self.head.next + self.head.next = newNode + def pop(self): + if self.head.next: + val = self.head.next.data + self.head.next = self.head.next.next + return val + else: + return None + a_stack = Stack() while True: diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..d7adf7a03 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,25 @@ +""" +Created on Sun Feb 8 16:20:59 2026 + +@author: rishigoswamy + +# Time Complexity: +# append : O(n) (traverses to the end of the list) +# find : O(n) (linear search) +# remove : O(n) (linear traversal to find the key) +# +# Space Complexity: +# O(n), where n is the number of nodes in the linked list. + +""" + 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): @@ -18,6 +35,18 @@ def append(self, data): Takes O(n) time. """ + if not self.head: + self.head = ListNode(data=data) + return + + ptr = self.head + while ptr.next: + ptr = ptr.next + + ptr.next = ListNode(data=data) + + + def find(self, key): """ Search for the first element with `data` matching @@ -25,8 +54,33 @@ def find(self, key): Takes O(n) time. """ + ptr = self.head + while ptr: + if ptr.data == key: + return ptr + ptr = ptr.next + + return None + def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + + dummy = ListNode(None, None) + dummy.next = self.head + + prev = dummy + curr = prev.next + while curr: + if curr.data == key: + prev.next = curr.next + self.head = dummy.next + return + prev = curr + curr = curr.next + + return + +