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
15 changes: 13 additions & 2 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@ 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 = []

def isEmpty(self):
return len(self.stack) == 0

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

def pop(self):

if self.isEmpty():
return "Stack is Empty"

return self.stack.pop()

def peek(self):
if self.isEmpty():
return "Stack is Empty"
return self.stack[-1]

def size(self):


return len(self.stack)
def show(self):
return self.stack


s = myStack()
Expand Down
15 changes: 15 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@ def __init__(self, data):

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

def push(self, data):
if self.head == None:
self.head = Node(data)
else:
nw = Node(data)
nw.next = self.head
self.head = nw


def pop(self):
if self.head is not None:
ppn = self.head
self.head = self.head.next
ppn.next = None
return ppn.data
else:
return None

a_stack = Stack()
while True:
Expand Down
55 changes: 54 additions & 1 deletion Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ class ListNode:
"""
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):

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



class SinglyLinkedList:
def __init__(self):
Expand All @@ -13,20 +18,68 @@ def __init__(self):
self.head = None

def append(self, data):

if self.head == None:
self.head = ListNode(data)
return

curr = self.head
while curr.next:
curr = curr.next
curr.next = ListNode(data)




"""
Insert a new element at the end of the list.
Takes O(n) time.
"""

def find(self, key):

curr = self.head
while (curr):
if (curr.data == key):
return curr

curr = curr.next
return None
"""
Search for the first element with `data` matching
`key`. Return the element or `None` if not found.
Takes O(n) time.
"""

def remove(self, key):
if self.head and self.head.data == key:
temp = self.head
self.head = self.head.next
temp.next = None
return

curr = self.head
while curr and curr.next:
if (curr.next.data == key):
temp = curr.next
curr.next = curr.next.next
temp.next = None
return
curr = curr.next
return None
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
def print_list(self):
curr = self.head

if curr is None:
print("List is empty")
return

while curr:
print(curr.data, end=" -> ")
curr = curr.next
print("None")