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
60 changes: 57 additions & 3 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,78 @@

### Stack with array


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 = []
self.MAX = 5


def size(self):
return len(self.stack)
#Time complexity O(1)


def isEmpty(self):
if self.size()==0:
return True
else:
return False
#Time complexity O(1)


def push(self, item):
if self.size()!=self.MAX:
self.stack.append(item)
else:
print("Stack is full")
return self.stack
#Time complexity O(1)


def pop(self):
if not self.isEmpty():
p=self.stack.pop()
return p
else:
print("Stack is empty")
return None
#Time complexity O(1)



def peek(self):

def size(self):
if not self.isEmpty():
return self.stack[-1]
else:
return None
#Time complexity O(1)


def show(self):

return self.stack
#Time complexity O(1)
# Space Complexity: O(n)

s = myStack()
s.push('1')
s.push('2')
s.push('3')
s.push('4')
s.push('5')
s.push('6')


print(s.pop())
print(s.pop())
print(s.pop())
print(s.pop())



print(s.show())
print(s.peek())


18 changes: 17 additions & 1 deletion Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@ def __init__(self, data):

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

def push(self, data):

new_node = Node(data)
new_node.next = self.top
self.top = new_node
self.count+=1
#Time complexity O(1)

def pop(self):
if self.top == None:
return None
else:
p = self.top.data
self.top = self.top.next
self.count -=1
return p
#Time complexity O(1)
#Space complexity O(n)

a_stack = Stack()
while True:
Expand Down
34 changes: 34 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 @@ -11,22 +13,54 @@ 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.
"""
new_node = ListNode(data)
if self.head == None:
self.head = new_node
return
current = self.head
while current.next:
current = current.next
current.next = new_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.

"""
current = self.head
while current:
if current.data == key:
return current.data
current = current.next
return None



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

current = self.head
previous_node = None

while current:
if current.data == key:
if previous_node:
previous_node.next=current.next
return
else:
self.head = current.next
previous_node = current
current=current.next