/* Sandra N. Kardaras-Flick 03/16/05 STACKS A program which allows the user to input integers, and returns those integers reversed. */ #include #include #define STACK_SIZE 50 #define TRUE 1 #define FALSE 0 typedef int boolean; void makeEmptyStack(void); boolean stackIsEmpty(void); boolean stackIsFull(void); void push(int i); int pop(void); void stackOverflow(void); void stackUnderflow(void); int contentsOfStack[STACK_SIZE]; int *top_ptr = &contentsOfStack[0]; int main() { int temp = 0; makeEmptyStack(); printf("\n\n\n\n\t Numeric entry:\t"); while ((temp = getchar()) != '\n') { if (isdigit(temp)) { temp = temp - '0'; push(temp); } } printf("Numeric entry reversed:\t"); while (!stackIsEmpty()) { printf("%d", pop()); } printf("\n\n\n\n"); return 0; } void makeEmptyStack(void) { top_ptr = &contentsOfStack[0]; } boolean stackIsEmpty(void) { return top_ptr == &contentsOfStack[0]; } boolean stackIsFull(void) { return top_ptr == &contentsOfStack[STACK_SIZE]; } void push(int i) { if (stackIsFull()) stackOverflow(); else *top_ptr++ = i; } int pop(void) { if (!stackIsEmpty()) return *--top_ptr; else stackUnderflow(); return 0; } void stackOverflow(void) { printf("\nStack Overflow\n"); exit(EXIT_FAILURE); } void stackUnderflow(void) { printf("\nStack Underflow\n"); exit(EXIT_FAILURE); }