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
18 changes: 18 additions & 0 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
#TC = isempty, pusing, popping, peeking, size would be O(1) and the showing/printing the stack would be o(n)
#Sc = o(n) since we have to store all elements even if we pop then, so number of elements pushed
# solved minstack on leetcode, in the append function, we append the minimum value of the two (one we currently are processing VS one that is already on top of min stack)
#if min stack is already empty, add the current one to it
#similarly, when we want to pop an element, we pop it from the minstack as well, since we build both the arrays simultaneously


class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):
self.stack = [] #created an array to store

def isEmpty(self):
return len(self.stack) == 0 #if len is 0, then it is empty

def push(self, item):
self.stack.append(item) #just append


def pop(self):
if self.isEmpty():
return "Empty stack" #check if its already empyt before popping
return self.stack.pop()


def peek(self):
return self.stack[-1] #return top element

def size(self):
return len(self.stack) #return len

def show(self):
return self.stack


s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())

23 changes: 20 additions & 3 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
#TC o(1) for push pop elements
#SC o(n) for storing all the elements pushed
#



class Node:
def __init__(self, data):
self.data = data
self.next = None
self.next = None

class Stack:
def __init__(self):
self.top = None #set top to none initially


def push(self, data):

newNode = Node(data) #create a new node that will tka ein your val
newNode.next = self.top #assing the next of this new node as top val (for easy popping)
self.top = newNode #the new top is the latest node added

def pop(self):
if self.top is None:
return None #if already empty, then return none
poppedval = self.top.data #create a variable to store popped val
self.top = self.top.next #shift the top to next of the current top element
return poppedval



a_stack = Stack()
while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
#Give input as string if getting an EOF error. Give inppoppoput like "push 10" or "pop"
print('push <value>')
print('pop')
print('quit')
Expand Down
23 changes: 23 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = next

class SinglyLinkedList:
def __init__(self):
Expand All @@ -17,16 +19,37 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""
newNode = ListNode(data)
curr = self.head
while curr.next:
curr = curr.next
curr.next = newNode


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.
"""
curr = self.head
while curr:
if curr.data == key:
return curr
curr = curr.next #check further elements
return None #if not found yet


def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
prev = None
curr = self.head
while curr:
if curr.data == key:
prev.next = curr.next #bypass the current one
return
prev = curr
curr = curr.next