Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
28 changes: 28 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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:
Expand Down
54 changes: 54 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -18,15 +35,52 @@ 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
`key`. Return the element or `None` if not found.
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