class Stack: def init(self): self.stack = [] # Initialize an empty list to represent the stack
def push(self, item):
"""Push an item onto the stack."""
self.stack.append(item)
print(f"Item {item} pushed to the stack.")
def pop(self):
"""Pop an item from the stack."""
if not self.is_empty():
popped_item = self.stack.pop()
print(f"Item {popped_item} popped from the stack.")
else:
print("Stack is empty! Cannot pop.")
def display(self):
"""Display the elements of the stack."""
if not self.is_empty():
print("Current stack:", self.stack)
else:
print("Stack is empty!")
def is_empty(self):
"""Check if the stack is empty."""
return len(self.stack) == 0
my_stack = Stack()
my_stack.display()
my_stack.push(10) my_stack.push(20) my_stack.push(30)
my_stack.display()
my_stack.pop()
my_stack.display()
my_stack.pop() my_stack.pop()
my_stack.pop()
my_stack.display()