diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..9025f509c 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -2,23 +2,35 @@ 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(): + raise IndexError("Pop from an empty stack") + return self.stack.pop() + def peek(self): - + if self.isEmpty(): + raise IndexError("Peek from an empty stack") + return self.stack[-1] + def size(self): - + return len(self.stack) + def show(self): - + if self.isEmpty(): + return "stack is empty" + else: + return f"last element is {self.stack[-1]}" s = myStack() s.push('1') s.push('2') -print(s.pop()) print(s.show()) +print(s.pop()) diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..d6a57d492 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,32 +1,80 @@ - class Node: - def __init__(self, data): - self.data = data - self.next = None - + def __init__(self, data, next=None): + self.data = data + self.next = None + + class Stack: def __init__(self): - - def push(self, data): - + self.top = None + + def push(self, val): + node = Node(val) + # Insert node at the start of the linked list + node.next = self.top + self.top = node + def pop(self): - + if self.top is None: + print("Stack is empty") + return None + top_element = self.top + self.top = top_element.next + return top_element.data + + def size(self): + stack_size = 0 + current_node = self.top + while current_node: + stack_size += 1 + current_node = current_node.next + return stack_size + + def print_stack(self): + current_node = self.top + while current_node: + print('||',current_node.data,'||') + current_node = current_node.next + print('-------------') + + def get_top(self): + if self.top is None: + return None + return self.top.data + + def is_empty(self): + return self.top is None + + + a_stack = Stack() -while True: - #Give input as string if getting an EOF error. Give input like "push 10" or "pop" - print('push ') - print('pop') - print('quit') - 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() - if operation == 'push': - a_stack.push(int(do[1])) - elif operation == 'pop': - popped = a_stack.pop() - if popped is None: - print('Stack is empty.') - else: - print('Popped value: ', int(popped)) - elif operation == 'quit': - break +a_stack.push(1) +a_stack.push(2) +a_stack.push(3) +a_stack.push(4) +a_stack.print_stack() +print('Size - ', a_stack.size()) +print("pooped element - ", a_stack.pop()) +a_stack.print_stack() +a_stack.get_top() +print('is stack empty? ', a_stack.is_empty()) +print('Size - ', a_stack.size()) + +# while True: +# #Give input as string if getting an EOF error. Give input like "push 10" or "pop" +# print('push ') +# print('pop') +# print('quit') +# 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() +# if operation == 'push': +# a_stack.push(int(do[1])) +# elif operation == 'pop': +# popped = a_stack.pop() +# if popped is None: +# print('Stack is empty.') +# else: +# print('Popped value: ', int(popped)) +# elif operation == 'quit': +# break diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..bd3cf77a9 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,7 +3,9 @@ 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): """ @@ -17,16 +19,68 @@ 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 + 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 is None: + self.head = current.next + else: + previous.next = current.next + return True # Element removed + previous = current + current = current.next + return False + +if __name__ == "__main__": + sll = SinglyLinkedList() + + sll.append(1) + sll.append(2) + sll.append(3) + + found_node = sll.find(2) + if found_node: + print(f"Found: {found_node.data}") + else: + print("Not found.") + + result = sll.remove(2) + if result: + print("Element removed.") + else: + print("Element not found.") + + found_node = sll.find(2) + if found_node: + print(f"Found: {found_node.data}") + else: + print("Not found.") diff --git a/tempCodeRunnerFile.py b/tempCodeRunnerFile.py new file mode 100644 index 000000000..9025f509c --- /dev/null +++ b/tempCodeRunnerFile.py @@ -0,0 +1,36 @@ +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(): + raise IndexError("Pop from an empty stack") + return self.stack.pop() + + def peek(self): + if self.isEmpty(): + raise IndexError("Peek from an empty stack") + return self.stack[-1] + + def size(self): + return len(self.stack) + + def show(self): + if self.isEmpty(): + return "stack is empty" + else: + return f"last element is {self.stack[-1]}" + +s = myStack() +s.push('1') +s.push('2') +print(s.show()) +print(s.pop())