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
36 changes: 23 additions & 13 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
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):
# Please read sample.java file before starting.
# Kindly include Time and Space complexity at top of each file
def __init__(self):
self.st = []

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

def peek(self):
def push(self, item):
self.st.append(item)

def size(self):

def show(self):
def pop(self):
if self.isEmpty():
return
return self.st.pop()

def peek(self):
if self.isEmpty():
return None
return self.st[-1]

def size(self):
return len(self.st)

def show(self):
return self.st


s = myStack()
Expand Down
14 changes: 13 additions & 1 deletion Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@ def __init__(self, data):

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

def push(self, data):
newNode = Node(data)
if self.top is None:
self.top = newNode
return
newNode.next = self.top
self.top = newNode

def pop(self):

if self.top is None:
return None
popped = self.top.data
self.top = self.top.next
return popped

a_stack = Stack()
while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
Expand Down
30 changes: 30 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,44 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""
if self.head is None:
self.head = ListNode(data)
return
curr = self.head
while curr.next:
curr = curr.next
curr.next = ListNode(data)

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
return None

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:
if prev is None:
self.head = curr.next
else:
prev.next = curr.next
return True
prev = curr
curr = curr.next

return False