-
Notifications
You must be signed in to change notification settings - Fork 523
Open
Description
class Stack:
"""A Last-In, First-Out (LIFO) data structure."""
def init(self):
self.items = []
def push(self, item):
"""Adds an item to the top of the stack."""
self.items.append(item)
def pop(self):
"""Removes and returns the item at the top of the stack."""
if not self.is_empty():
return self.items.pop()
raise IndexError("pop from empty stack")
def is_empty(self):
"""Checks if the stack is empty."""
return len(self.items) == 0
Example Usage:
my_stack = Stack()
my_stack.push('A')
my_stack.push('B')
print("Popped item:", my_stack.pop()) # Output: B
print("Is stack empty?", my_stack.is_empty()) # Output: False
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels