Skip to main content

FIFO vs LIFO Queue & Stack Visualizer – Free Interactive JavaScript Data Structure Demo

FIFO vs LIFO is a free, copy-paste ready side-by-side interactive data structure visualizer built with pure HTML, CSS, and vanilla JavaScript — plus canvas-confetti CDN. It renders a two-column CSS Grid (grid-template-columns: 1fr 1fr) with glassmorphism cards: backdrop-filter: blur(16px), background: rgba(255,255,255,0.25), border: 1px solid rgba(255,255,255,0.18). Each card hovers translateY(-12px) on mouse-over. On page load, an auto-demo uses window.onload with forEach + i*800ms setTimeout to auto-add Apple, Banana, and Cherry to both structures simultaneously.

The Queue (FIFO) container uses display:flex; flex-direction:row. Enqueue appends a 90×90px .box div with background: hsl(count*40, 70%, 55%) — cycling through the full color spectrum — and entrance animation slideIn 0.6s ease-out (translateX(300px) → 0). Dequeue applies slideOut 0.6s ease-in forwards to children[0] (translateX(0) → -300px), then removes the DOM node after 600ms. The Stack (LIFO) uses display:flex; flex-direction:column-reverse — new appendChild() items appear visually at the top automatically. Push uses riseUp 0.6s cubic-bezier(0.68, -0.55, 0.27, 1.55) (translateY(200px) → 0) — the negative/over-1 cubic-bezier values create an overshoot spring. Pop applies fallOut (translateY(-300px) scale(0.5) opacity:0) to lastElementChild. Both structures fire confetti({particleCount:150, spread:80, origin:{y:0.6}}) when reaching 10 items.

What's Included in the Download

How the Stack Column-Reverse Trick Works

The #stack container uses flex-direction: column-reverse. In a normal column flex container, appendChild() adds items to the visual bottom. With column-reverse, the most recently appended child appears at the visual top of the container — exactly matching how a stack should look (last in = on top). This requires zero JavaScript reordering. When pop() removes lastElementChild (the most recently pushed DOM node), it happens to be the visually topmost item. The fallOut animation (translateY(-300px) scale(0.5)) launches it upward, reinforcing the mental model of "popping off the top."

FIFO vs LIFO: Real-World Use Cases

FIFO (Queue) is used in: print queues (first document sent = first printed), CPU task scheduling (round-robin), BFS graph traversal (breadth-first search processes nodes level by level), message queues (Kafka, RabbitMQ), and HTTP request queues. LIFO (Stack) is used in: function call stacks (most recent function = first to return), undo/redo systems (most recent action = first to undo), DFS graph traversal, expression parsing (balanced parentheses checking), and browser history (back button visits most recent page first).

License

Released under Creative Commons Attribution 4.0 International (CC BY 4.0). Free for personal and commercial use with attribution to Coodeverse.

Source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>FIFO vs LIFO - Interactive Demo</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;900&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/confetti.browser.min.js"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;900&display=swap');

        :root {
            --bg: #f8fafc;
            --card: rgba(255, 255, 255, 0.25);
            --text: #1e293b;
            --fifo: #6366f1;
            --lifo: #ec4899;
            --shadow: 0 20px 40px rgba(0,0,0,0.1);
        }

        .dark {
            --bg: #0f172a;
            --card: rgba(15, 23, 42, 0.4);
            --text: #e2e8f0;
            --shadow: 0 20px 40px rgba(0,0,0,0.4);
        }

        * { margin:0; padding:0; box-sizing:border-box; }

        body {
            font-family: 'Inter', sans-serif;
            background: var(--bg);
            color: var(--text);
            margin: 0;
            padding: 20px;
            transition: all 0.4s;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        button.toggle-btn {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 12px 24px;
            background: rgba(100,100,100,0.3);
            border: none;
            border-radius: 50px;
            backdrop-filter: blur(12px);
            color: white;
            font-weight: 700;
            cursor: pointer;
            z-index: 1000;
        }

        .container {
            max-width: 1300px;
            width: 100%;
            text-align: center;
            margin-top: 2rem;
        }

        h1 {
            font-size: 3.8rem;
            background: linear-gradient(90deg, #6366f1, #ec4899, #10b981);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 900;
            margin: 2rem 0 0.5rem;
        }

        header p {
            font-size: 1.5rem;
            opacity: 0.85;
            margin-bottom: 2rem;
        }

        .grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 3rem;
            margin: 3rem 0;
        }

        .card {
            background: var(--card);
            backdrop-filter: blur(16px);
            border-radius: 28px;
            padding: 2.5rem;
            box-shadow: var(--shadow);
            border: 1px solid rgba(255,255,255,0.18);
            transition: transform 0.4s ease;
        }

        .card:hover { transform: translateY(-12px); }

        h2 { font-size: 2.4rem; margin-bottom: 1.5rem; font-weight: 900; }

        .fifo h2 {
            background: linear-gradient(135deg, #6366f1, #8b5cf6);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .lifo h2 {
            background: linear-gradient(135deg, #ec4899, #f43f5e);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .display {
            min-height: 420px;
            background: rgba(0,0,0,0.15);
            border-radius: 20px;
            margin: 1.5rem 0;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        #queue { display: flex; gap: 16px; flex-wrap: nowrap; }

        #stack { display: flex; flex-direction: column-reverse; align-items: center; gap: 12px; }

        .box {
            width: 90px;
            height: 90px;
            border-radius: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 900;
            font-size: 1.8rem;
            color: white;
            box-shadow: 0 15px 30px rgba(0,0,0,0.3);
            animation: popIn 0.6s ease-out;
        }

        input {
            padding: 14px 20px;
            width: 220px;
            border: none;
            border-radius: 16px;
            background: rgba(255,255,255,0.15);
            backdrop-filter: blur(10px);
            color: var(--text);
            text-align: center;
            font-size: 1.1rem;
            margin: 1rem 0;
        }

        button {
            padding: 14px 32px;
            margin: 8px;
            border: none;
            border-radius: 16px;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.3s;
            backdrop-filter: blur(10px);
            color: white;
        }

        .enqueue, .dequeue { background: rgba(99, 102, 241, 0.85); }
        .push, .pop        { background: rgba(236, 72, 153, 0.85); }

        button:hover { transform: scale(1.08); }

        .info { margin-top: 1.2rem; font-size: 1.2rem; opacity: 0.9; }

        .status {
            font-weight: bold;
            color: #10b981;
            font-size: 1.3rem;
            min-height: 2rem;
            display: block;
            margin-top: 0.8rem;
        }

        button.reset {
            padding: 18px 48px;
            font-size: 1.3rem;
            background: #64748b;
            margin: 3rem auto;
            display: block;
            border-radius: 50px;
        }

        @media (max-width: 968px) {
            .grid { grid-template-columns: 1fr; }
            h1 { font-size: 3rem; }
        }

        @keyframes popIn {
            from { transform: scale(0) rotate(360deg); }
            to   { transform: scale(1); }
        }
        @keyframes slideIn {
            from { transform: translateX(300px); opacity: 0; }
            to   { transform: translateX(0); opacity: 1; }
        }
        @keyframes slideOut {
            from { transform: translateX(0); }
            to   { transform: translateX(-300px); opacity: 0; }
        }
        @keyframes riseUp {
            from { transform: translateY(200px); opacity: 0; }
            to   { transform: translateY(0); opacity: 1; }
        }
        @keyframes fallOut {
            to { transform: translateY(-300px) scale(0.5); opacity: 0; }
        }
    </style>
</head>
<body>
    <button class="toggle-btn" onclick="document.body.classList.toggle('dark')">Dark Mode</button>

    <div class="container">
        <header>
            <h1>FIFO vs LIFO</h1>
            <p>Interactive Queue & Stack Visualizer</p>
        </header>

        <div class="grid">
            <!-- FIFO -->
            <div class="card fifo">
                <h2>FIFO - Queue</h2>
                <div class="display"><div id="queue"></div></div>
                <input type="text" id="queueInput" placeholder="Enter value (e.g. Apple)" value="A"/>
                <div>
                    <button class="enqueue" onclick="enqueue()">Enqueue</button>
                    <button class="dequeue" onclick="dequeue()">Dequeue</button>
                </div>
                <div class="info">Size: <strong id="qsize">0</strong> | <span id="qstatus" class="status"></span></div>
                <div class="tagline">First In → First Out</div>
            </div>

            <!-- LIFO -->
            <div class="card lifo">
                <h2>LIFO - Stack</h2>
                <div class="display"><div id="stack"></div></div>
                <input type="text" id="stackInput" placeholder="Enter value (e.g. Book)" value="B"/>
                <div>
                    <button class="push" onclick="push()">Push</button>
                    <button class="pop" onclick="pop()">Pop</button>
                </div>
                <div class="info">Size: <strong id="ssize">0</strong> | <span id="sstatus" class="status"></span></div>
                <div class="tagline">Last In → First Out</div>
            </div>
        </div>

        <button class="reset" onclick="resetAll()">Reset Everything</button>
    </div>

    <script>
        let queueCount = 0, stackCount = 0;

        function celebrate() {
            confetti({ particleCount: 150, spread: 80, origin: { y: 0.6 } });
        }

        function updateSizes() {
            document.getElementById('qsize').textContent = document.getElementById('queue').children.length;
            document.getElementById('ssize').textContent = document.getElementById('stack').children.length;
        }

        function enqueue() {
            const val = document.getElementById('queueInput').value.trim() || `Q${++queueCount}`;
            const q = document.getElementById('queue');
            const box = document.createElement('div');
            box.className = 'box';
            box.textContent = val;
            box.style.background = `hsl(${queueCount * 40}, 70%, 55%)`;
            box.style.animation = 'slideIn 0.6s ease-out';
            q.appendChild(box);
            document.getElementById('qstatus').textContent = `Enqueued "${val}"`;
            updateSizes();
            if (q.children.length === 10) celebrate();
        }

        function dequeue() {
            const q = document.getElementById('queue');
            if (q.children.length === 0) {
                document.getElementById('qstatus').textContent = 'Queue is empty!';
                return;
            }
            const removed = q.children[0].textContent;
            q.children[0].style.animation = 'slideOut 0.6s ease-in forwards';
            setTimeout(() => { q.removeChild(q.children[0]); updateSizes(); }, 600);
            document.getElementById('qstatus').textContent = `Dequeued "${removed}"`;
        }

        function push() {
            const val = document.getElementById('stackInput').value.trim() || `S${++stackCount}`;
            const s = document.getElementById('stack');
            const box = document.createElement('div');
            box.className = 'box';
            box.textContent = val;
            box.style.background = `hsl(${stackCount * 60 + 300}, 70%, 55%)`;
            box.style.animation = 'riseUp 0.6s cubic-bezier(0.68, -0.55, 0.27, 1.55)';
            s.appendChild(box);
            document.getElementById('sstatus').textContent = `Pushed "${val}"`;
            updateSizes();
            if (s.children.length === 10) celebrate();
        }

        function pop() {
            const s = document.getElementById('stack');
            if (s.children.length === 0) {
                document.getElementById('sstatus').textContent = 'Stack is empty!';
                return;
            }
            const removed = s.lastElementChild.textContent;
            s.lastElementChild.style.animation = 'fallOut 0.6s ease-in forwards';
            setTimeout(() => { s.removeChild(s.lastElementChild); updateSizes(); }, 600);
            document.getElementById('sstatus').textContent = `Popped "${removed}"`;
        }

        function resetAll() {
            document.getElementById('queue').innerHTML = '';
            document.getElementById('stack').innerHTML = '';
            document.getElementById('qstatus').textContent = '';
            document.getElementById('sstatus').textContent = '';
            updateSizes();
        }

        window.onload = () => {
            ['Apple', 'Banana', 'Cherry'].forEach((v, i) => {
                setTimeout(() => {
                    document.getElementById('queueInput').value = v;
                    enqueue();
                    document.getElementById('stackInput').value = v;
                    push();
                }, i * 800);
            });
        };
    </script>
</body>
</html>

Frequently Asked Questions

Common questions about the free FIFO vs LIFO Queue and Stack Visualizer project.

Yes — 100% free under Creative Commons Attribution 4.0. No sign-up, no payment. Click Download Project and use it for studying data structures, teaching DSA, coding interview preparation, or portfolio projects with attribution to Coodeverse.

FIFO (First-In First-Out) = Queue: first added is first removed. Operations: enqueue (add to back), dequeue (remove from front). Like a checkout line. Used in: print queues, BFS traversal, CPU task scheduling, message queues. LIFO (Last-In First-Out) = Stack: last added is first removed. Operations: push (add to top), pop (remove from top). Like a stack of plates. Used in: function call stacks, undo/redo systems, DFS traversal, expression parsing, browser history.

Set the animation before appending: box.style.animation = 'slideIn 0.6s ease-out'. Define: @keyframes slideIn { from { transform: translateX(300px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }. The box enters from the right (300px), simulating arrival at the back of the queue. For color variety: box.style.background = 'hsl(' + (count * 40) + ', 70%, 55%)' — incrementing 40° per item cycles through the spectrum.

Apply exit animation to children[0]: q.children[0].style.animation = 'slideOut 0.6s ease-in forwards'. Define: @keyframes slideOut { from { transform: translateX(0); } to { transform: translateX(-300px); opacity: 0; } }. The forwards fill mode holds the element off-screen during the timeout. Remove from DOM after matching the animation duration: setTimeout(() => { q.removeChild(q.children[0]); }, 600).

Use animation: riseUp 0.6s cubic-bezier(0.68, -0.55, 0.27, 1.55). The negative second value (-0.55) and over-1 fourth value (1.55) create an overshoot — the element rises past its target then springs back. Keyframe: @keyframes riseUp { from { transform: translateY(200px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }. For pop: @keyframes fallOut { to { transform: translateY(-300px) scale(0.5); opacity: 0; } } — scaling down as it flies up makes the exit feel dynamic.

Use flex-direction: column-reverse on the stack container. Normal column flex places new appendChild() items at the visual bottom. With column-reverse, the most recently appended child appears at the visual top — matching the stack mental model with zero JavaScript reordering. When pop() removes lastElementChild (the newest DOM node), it's the visually topmost item.

Browse coodeverse.com/projects — Binary Search Visualizer (L/Mid/R pointer animation), Animated 3D Pie Chart, Pro Chronometer, Word Typing Adventure, VORTEX Canvas, and more. All 100% free with complete HTML CSS JavaScript source code.