University of Massachusetts, Lowell**We aren't endorsed by this school
Course
MUSIC 41.202
Subject
Computer Science
Date
Jan 6, 2025
Pages
2
Uploaded by BrigadierProton24583
Suganya G7 / 106. StackA Stackfollows the LIFO (Last In, First Out)principle, where the last element added is the first to beremoved.Implementation:class Stack: def __init__(self): self.stack = [] def push(self, data): self.stack.append(data) def pop(self): if not self.is_empty(): return self.stack.pop() return "Stack is empty" def peek(self): if not self.is_empty(): return self.stack[-1] return "Stack is empty" def is_empty(self): return len(self.stack) == 0 # Example usage stack = Stack() stack.push(5) stack.push(10) print(stack.pop()) # Output: 10 print(stack.peek()) # Output: 5
Suganya G8 / 107. QueueA Queueoperates on the FIFO (First In, First Out)principle, where the first element added is the first to beremoved.Implementation:from collections import deque class Queue: def __init__(self): self.queue = deque() def enqueue(self, data): self.queue.append(data) def dequeue(self): if not self.is_empty(): return self.queue.popleft() return "Queue is empty" def is_empty(self): return len(self.queue) == 0 # Example usage queue = Queue() queue.enqueue(1) queue.enqueue(2) print(queue.dequeue()) # Output: 1 print(queue.dequeue()) # Output: 2