typestar

An array-backed stack in C

A fixed-capacity stack with push and pop.

#define CAP 64

struct Stack {
    int items[CAP];
    int top;
};

void push(struct Stack *s, int value) {
    if (s->top < CAP)
        s->items[s->top++] = value;
}

int pop(struct Stack *s) {
    return s->top > 0 ? s->items[--s->top] : -1;
}

How it works

  1. top tracks the next free slot.
  2. push writes then advances top.
  3. pop retreats top and returns the value.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
222
Tokens
86
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 52 seconds.

Type this snippet

Step 4 of 5 in Lists & stacks, step 9 of 20 in Data structures & algorithms.

← Previous Next →