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
31 changes: 20 additions & 11 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
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 __init__(self):
self.ls=[]

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

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

def pop(self):

def pop(self):
if self.isEmpty():
return None
return self.ls.pop()

def peek(self):
def peek(self):
if len(self.ls)>0:
return self.ls[-1]
return None

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

def show(self):
def show(self):
return self.ls


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

class Stack:
def __init__(self):
self.head=Node(-1)

def push(self, data):

node=Node(data)
node.next=self.head.next
self.head.next=node
def pop(self):
if self.head.next is None:
return None
q=self.head.next
self.head.next=q.next
return q.data



a_stack = Stack()
while True:
Expand Down
28 changes: 28 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,42 @@ 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!=None:
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!=None:
if curr.data==key:
return key
curr = curr.next
return None



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