#include /* C example */ #include #include "stack.h" #define STACK_MAX_SIZE 100 static int a[STACK_MAX_SIZE]; static size_t n = 0; /* number of values in the stack; stack initially empty */ /* Push a value onto the stack. */ void push(int i) { if (n == STACK_MAX_SIZE) { /* overflow */ fprintf(stderr, "Can't push when size %u == capacity %u.\n", n, STACK_MAX_SIZE); exit(EXIT_FAILURE); } a[n++] = i; } /* Pop a value off the stack. */ int pop(void) { if (n == 0) { /* underflow */ fprintf(stderr, "Can't pop when size %u == 0.\n", n); exit(EXIT_FAILURE); } return a[--n]; }