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: 9 additions & 4 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ 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 self.stack == []

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

def pop(self):

return self.stack.pop()

def peek(self):

return self.stack[-1]

def size(self):

return len(self.stack)

def show(self):

return self.stack

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

class Stack:
def __init__(self):
self.stack = Node(None)

def push(self, data):
new_node = Node(data)
new_node.next = self.stack
self.stack = new_node

def pop(self):
if self.stack.data is None:
return None
popped_data = self.stack.data
self.stack = self.stack.next
return popped_data

a_stack = Stack()
while True:
Expand Down
30 changes: 29 additions & 1 deletion Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,58 @@ 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):
"""
Create a new singly-linked list.
Takes O(1) time.
"""
self.node = ListNode()
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 not self.head:
self.head = new_node
else:
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.
"""
current = self.head
previous = None
while current:
if current.data == key:
if previous:
previous.next = current.next
else:
self.head = current.next
return
previous = current
current = current.next