Ready — enter an integer to push
Speed
🎨 Customize Colors
Scene Background
Element Color
Value Color
Top Pointer Color

📥 Stack (LIFO)

A Stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. The last element added to the stack is the first one to be removed. Imagine a stack of plates—you add to the top and take from the top.

⏱ Access: O(n) ⏱ Search: O(n) ⏱ Push/Pop: O(1) 💾 Space: O(n)
Python
class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        if not self.is_empty():
            return self.items.pop()

    def is_empty(self):
        return len(self.items) == 0
JavaScript
class Stack {
    constructor() {
        this.items = [];
    }

    push(element) {
        this.items.push(element);
    }

    pop() {
        if (this.isEmpty()) return null;
        return this.items.pop();
    }

    isEmpty() {
        return this.items.length === 0;
    }
}