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
toptracks the next free slot.pushwrites then advancestop.popretreatstopand returns the value.
Keywords and builtins used here
Stackifintpoppushreturnstructvoid
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.
Step 4 of 5 in Lists & stacks, step 9 of 20 in Data structures & algorithms.