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
42 changes: 39 additions & 3 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file

#Time and Space Complexity
# Overall Space Complexity = O(N) where N is the MAX_SIZE of 1000
# __init__ function has the time and space complexity of O(N)
# isEmpty function has the time and space complexity of O(1)
# push function has the time and space complexity of O(1)
# pop function has the time and space complexity of O(1)
# peek function has the time and space complexity of O(1)
# size function has the time and space complexity of O(1)
# show function has the time and space complexity of O(M) where M dentoes the number of elements in the stack


def __init__(self):
self.MAX_SIZE=1000
self.array=[None]*self.MAX_SIZE
self.top=-1

def isEmpty(self):
if self.top==-1:
return True
return False

def push(self, item):
if self.top<self.MAX_SIZE-1:
self.top+=1
self.array[self.top]=item
else:
print("Stack Overflow")
return False

def pop(self):


if self.top>-1:
value=self.array[self.top]
self.top-=1
return value
else:
print("Stack Underflow")
return False

def peek(self):
if self.top==-1:
return False
else:
return self.array[self.top]

def size(self):
return self.top+1

def show(self):

return self.array[0: self.top+1][::-1]


s = myStack()
s.push('1')
Expand Down
16 changes: 15 additions & 1 deletion Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@

# Time and Space Complexity
# Overall Space Complexity is O(N) where N is the number of elements in stack
# __init__ function has time and space complexity of O(1)
# push function has time and space complexity of O(1)
# pop function has time and space complexity of O(1)
class Node:
def __init__(self, data):
self.data = data
self.next = None

class Stack:
def __init__(self):
self.top=None

def push(self, data):
newNode=Node(data)
newNode.next=self.top
self.top=newNode

def pop(self):
if self.top==None:
return None
else:
value=self.top.data
self.top=self.top.next
return value

a_stack = Stack()
while True:
Expand Down
42 changes: 42 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Time and Space Complexity
# Overall Space Complexity is O(N) where N is total number of elements in the list
# append function takes O(N) time complexity and O(1) space complexity
# find function takes O(N) time complexity and O(1) space complexity
# remove function takes O(N) time complexity and O(1) space complexity

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 +25,50 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""
newNode=ListNode(data)
if self.head==None:
self.head=newNode
else:
a=self.head
while a.next!=None:
a=a.next
a.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.
"""
a=self.head
while a!=None:
if a.data==key:
return a
a=a.next
return None

def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
a=self.head
if self.head==None:
return None
if self.head.data==key:
self.head=self.head.next
return None
while a.next!=None:
if a.next.data==key:
a.next=a.next.next
return None
a=a.next

ll = SinglyLinkedList()

ll.append(10)
ll.append(20)
ll.append(30)
found = ll.find(20)
print(found.data)
ll.remove(20)