diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..ec991c815 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,48 @@ +# Time Complexity: O(1) +# Space Complexity: O(1) +""" +Init class with empty stack of default size 1000 and starts top with -1 +push will increment the top and use that index to add the element +pop will use top val to decrement the index +""" 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 __init__(self, capacity=1000): + self.stack = [None] * capacity + self.top = -1 + + def isEmpty(self): + if self.top == -1: + return True + return False - def pop(self): - + def push(self, item): + if self.top == len(self.stack) - 1: + return "Stack Overflow" + self.top += 1 + self.stack[self.top] = item + + def pop(self): + if self.isEmpty(): + return "Stack Underflow" + self.top -= 1 + return self.stack[self.top + 1] - def peek(self): + def peek(self): + if self.isEmpty(): + return "Stack Underflow" + return self.stack[self.top] - def size(self): + def size(self): + return self.top + 1 - def show(self): + def show(self): + return self.stack s = myStack() s.push('1') s.push('2') print(s.pop()) -print(s.show()) +# print(s.show()) + + diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..4d69c6066 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,10 @@ - +""" +Init class with head with null +Push will add the node to the top of the list +Pop will point the pointer to the next node +Time complexity O(1) +Space O(n) +""" class Node: def __init__(self, data): self.data = data @@ -6,10 +12,27 @@ def __init__(self, data): class Stack: def __init__(self): + self.head = None def push(self, data): + temp = Node(data) + temp.next = self.head + self.head = temp def pop(self): + if self.head == None: + return None + + temp = self.head + self.head = temp.next + return temp.data + + def show(self): + temp = self.head + while temp: + print(temp.data, end=" -> ") + temp = temp.next + print("None") a_stack = Stack() while True: @@ -17,6 +40,7 @@ def pop(self): print('push ') print('pop') print('quit') + print('print') do = input('What would you like to do? ').split() #Give input as string if getting an EOF error. Give input like "push 10" or "pop" operation = do[0].strip().lower() @@ -28,5 +52,7 @@ def pop(self): print('Stack is empty.') else: print('Popped value: ', int(popped)) + elif operation == 'print': + print(a_stack.show()) elif operation == 'quit': break diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..123ee6cd4 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,16 @@ +# Initilaized the class with head and tail +# append method will check if there is any head avaiable and creates head if not none +# used tail pointer to append to the end of the linked list without iterating through full list +# updating tail everytime when inserting +# find method will start from the head node and the iterate through the list returns the val if found else None +# remove method will find the key first by maintaing previous node 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): @@ -11,12 +19,28 @@ def __init__(self): Takes O(1) time. """ self.head = None + self.tail = None 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 + self.tail = new_node + return + + # curr = self.head + + # while curr: + # curr = curr.next + # curr.next = new_node + + self.tail.next = new_node + self.tail = new_node def find(self, key): """ @@ -24,9 +48,37 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + + curr = self.head + + while curr: + + if curr.data == key: + return curr.data + curr = curr.next + return None def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + + curr = self.head + prev = None + while curr: + if curr.data == key: + if prev == None: + self.head = curr.next + else: + prev.next = curr.next + + + if curr is self.tail: + self.tail = prev + return True + + prev = curr + curr = curr.next + + return False