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
13 changes: 12 additions & 1 deletion 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 None

return self.stack.pop()

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

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

def show(self):
return self.stack


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

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

def push(self, data):

new_node = Node(data)
new_node.next = self.top
self.top = new_node

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:
Expand Down
66 changes: 65 additions & 1 deletion 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,78 @@ 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 is 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
current = current.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 the head is the node we want to remove
if self.head.data == key:
self.head = self.head.next
return
current = self.head
while current:
if current.next.data == key:
current.next = current.next.next
return
current = current.next

def print_list(self):
current = self.head

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

print("None")


# Test
linked_list = SinglyLinkedList()

linked_list.append(10)
linked_list.append(20)
linked_list.append(30)
linked_list.append(40)

print("Original list:")
linked_list.print_list()

print("\nFinding 20:")
node = linked_list.find(20)
print(node.data if node else None)

print("\nRemoving 20:")
linked_list.remove(20)
linked_list.print_list()

print("\nRemoving head (10):")
linked_list.remove(10)
linked_list.print_list()