Q#8. Write a program to show the operations of stack.
class Stack:
def __init__(self):
[Link] = []
def push(self, item):
[Link](item)
def pop(self):
return [Link]() if [Link] else "Stack is empty"
def peek(self):
return [Link][-1] if [Link] else "Stack is empty"
def display(self):
return [Link]
# Demonstrate stack operations
stack = Stack()
[Link](1)
[Link](2)
[Link](3)
print("Stack after pushing 1, 2, 3:", [Link]())
print("Peek top item:", [Link]())
print("Popped item:", [Link]())
print("Stack after popping an item:", [Link]())
print("Popped item:", [Link]())
print("Popped item:", [Link]())
print("Popped item (should be empty):", [Link]())
print("Stack after popping all items:", [Link]())
Output:
Stack after pushing 1, 2, 3: [1, 2, 3]
Peek top item: 3
Popped item: 3
Stack after popping an item: [1, 2]
Popped item: 2
Popped item: 1
Popped item (should be empty): Stack is empty
Stack after popping all items: []