Skip to content

Conversation

@pearmini
Copy link
Collaborator

@pearmini pearmini commented Dec 9, 2025

/**
 * ============================================================================
 * =                                Stack                                     =
 * ============================================================================
 *
 * This example demonstrates a classic Stack data structure with push and pop
 * operations. A stack follows the LIFO (Last In, First Out) principle. The
 * following is a simple demonstration of how it looks like.
 */

const [stack, setStack] = recho.state(new Stack());

//➜ ""
echo(stack.items.join(""));

// Push button - adds a random alphabet character to the stack
recho.button("Push", push);

// Pop button - removes the top element from the stack
recho.button("Pop", pop);

/**
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *                               Implementation
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * The stack is implemented as a class with a few methods:
 *
 * - `push(element)`: adds an element to the stack
 * - `pop()`: removes the top element from the stack
 * - `clone()`: creates a deep copy of the stack
 *
 */

class Stack {
  constructor() {
    this.items = [];
  }

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

  pop() {
    if (this.items.length === 0) return undefined;
    return this.items.pop();
  }

  clone() {
    const cloned = new Stack();
    cloned.items = [...this.items];
    return cloned;
  }
}

/**
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *                               Helper Functions
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * The following are helper functions to push and pop elements from the stack,
 * and a function to generate a random alphabet character (A-z).
 */

function push() {
  setStack((s) => {
    const cloned = s.clone();
    cloned.push(randomAlphabet());
    return cloned;
  });
}

function pop() {
  setStack((s) => {
    const cloned = s.clone();
    cloned.pop();
    return cloned;
  });
}

// Function to generate a random alphabet character (A-z)
function randomAlphabet() {
  // Generate random character from A (65) to z (122)
  // This includes A-Z (65-90), some special chars (91-96), and a-z (97-122)
  const code = Math.floor(Math.random() * (122 - 65 + 1)) + 65;
  return String.fromCharCode(code);
}

@vercel
Copy link

vercel bot commented Dec 9, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
recho-notebook Ready Ready Preview Comment Dec 9, 2025 3:47pm

@pearmini pearmini added the Example Add a new example label Dec 9, 2025
@pearmini pearmini merged commit 4133af5 into main Dec 9, 2025
3 checks passed
@pearmini pearmini deleted the stack-example branch December 9, 2025 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Example Add a new example

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants