</>EmbeddedInterview

Implement a stack using an array and using a linked list.

Easy"Coding: Linked Lists, Stacks & Queues"
#include <stdbool.h>
#include <stdlib.h>

/* array stack: fixed capacity, no allocation */
#define STACK_CAP 32
typedef struct { int data[STACK_CAP]; int top; } astack_t;   /* top = number of items */

bool as_push(astack_t *s, int v) { if (s->top == STACK_CAP) return false; s->data[s->top++] = v; return true; }
bool as_pop(astack_t *s, int *v) { if (s->top == 0) return false; *v = s->data[--s->top]; return true; }

/* linked-list stack: unbounded, push/pop at the head */
typedef struct snode { int v; struct snode *next; } snode_t;

bool ls_push(snode_t **top, int v)
{
    snode_t *n = malloc(sizeof *n);
    if (!n) return false;
    n->v = v; n->next = *top; *top = n;
    return true;
}

bool ls_pop(snode_t **top, int *v)
{
    if (!*top) return false;
    snode_t *n = *top; *v = n->v; *top = n->next; free(n);
    return true;
}

Points: check overflow and underflow and report them; the array version is deterministic and allocation-free (preferred on MCUs); the list version grows as needed but allocates per push. All operations are O(1).