Simvik

Fixed-size stack

Push and pop on an array-backed stack with overflow and underflow checks. An easy embedded C problem (Buffers and data structures), run and graded in the…

easy · Buffers and data structures · stack · lifo

Implement a last-in, first-out stack. A zero-initialised istack_t is empty.

  • stack_push adds v and returns true, or returns false when the stack is full.
  • stack_pop removes the most recently pushed value into *v and returns true, or returns false and leaves *v alone when the stack is empty.

Starter code

bool stack_push(istack_t *s, int32_t v) {
  return false;
}

bool stack_pop(istack_t *s, int32_t *v) {
  return false;
}

Example tests

Last in, first out

istack_t s = { 0 };
stack_push(&s, 1); stack_push(&s, 2); stack_push(&s, 3);
int32_t a = 0, b = 0;
stack_pop(&s, &a); stack_pop(&s, &b);
printf("%d %d\n", a, b);

Expected output:

3 2

Popping an empty stack

istack_t s = { 0 };
int32_t v = 77;
printf("%d %d\n", stack_pop(&s, &v), v);

Expected output:

0 77

Learn it first

More Buffers and data structures problems