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
62 changes: 44 additions & 18 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,50 @@
# Time Complexity:
# push() -> O(1)
# pop() -> O(1)
# peek() -> O(1)
# isEmpty() -> O(1)
# size() -> O(1)
# show() -> O(n)
# Space Complexity: O(n) where n is the number of elements in the stack

# Any problem you faced while coding this : no

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 list
self.stack = []

def isEmpty(self):
# Returns True if stack is empty, False otherwise
return len(self.stack) == 0

def push(self, item):
# Adds an item to the top of the stack
self.stack.append(item)

def pop(self):
# Removes and returns the top item of the stack
if self.isEmpty():
return "Stack is empty"
return self.stack.pop()

def peek(self):
# Returns the top item of the stack without removing it
if self.isEmpty():
return "Stack is empty"
return self.stack[-1]

def size(self):
# Returns the number of elements in the stack
return len(self.stack)

def show(self):
# Returns a list of all stack elements
return self.stack.copy()


s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())
print(s.pop()) # 2
print(s.show()) # ['1']
18 changes: 18 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ def __init__(self, data):

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


def push(self, data):
new_node= Node(data)
new_node.next= self.head
self.head= new_node



def pop(self):
if self.head is None:
return None
popped_item= self.head.data
self.head=self.head.next
return popped_item






a_stack = Stack()
while True:
Expand All @@ -22,6 +39,7 @@ def pop(self):
operation = do[0].strip().lower()
if operation == 'push':
a_stack.push(int(do[1]))

elif operation == 'pop':
popped = a_stack.pop()
if popped is None:
Expand Down
48 changes: 48 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@


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 @@ -11,22 +15,66 @@ def __init__(self):
Takes O(1) time.
"""
self.head = None


def append(self, data):
"""
Insert a new element at the end of the list.
Takes O(n) time.
"""
n_node=ListNode(data)

if self.head ==None:
self.head=n_node
return

cur=self.head
while cur.next!=None:
cur=cur.next
cur.next=n_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.
"""
temp=self.head

while temp:

if temp.data==key:
return temp
temp=temp.next

return None







def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""

temp=self.head
prev=None
while temp:
if temp.data==key:
if prev is None:
self.head = temp.next
else:
prev.next=temp.next
break
prev=temp
temp=temp.next