Skip to content

Stack code  #156

@akashdhakane327-crypto

Description

@akashdhakane327-crypto

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions