/* * the stack module */ #include #include "stack.h" static int stack[STACKSIZE]; /* the stack itself */ static int top = -1; /* index of top element of stack; -1 is empty */ /* * push a value onto the stack * exits on stack overflow */ void push(int val) { /* check for overflow */ if (top >= STACKSIZE-1){ fprintf(stderr, "Stack is full\n"); return; } /* push onto stack */ stack[++top] = val; return; } /* * pop a value from the stack * exits on stack underflow */ int pop(int *val) { /* check for an empty stack */ if (top < 0){ fprintf(stderr, "Stack is empty\n"); return(0); } /* pop from stack */ *val = stack[top--]; return(1); } /* * print the top of the stack and the elements on the stack */ void printstack(void) { int i; /* used to walk the stack */ /* print the top index */ printf("top = %d\n", top); /* now print the elements of the stack, top down */ for(i = top; i >= 0; i--) printf("%3d: %10d\n", i, stack[i]); }