/* * ISA 563, Spring 2011 * Copyleft by Muhammad Abdulla */ #include #include typedef enum {false, true} bool; #define MAX 100 #define MAXLINE 32 int stack_idx = 0; int stack[MAX]; void stack_push(int item) { if ( stack_idx < MAX ) { stack[stack_idx++] = item; } else { fprintf(stderr, "stack is full, returning.\n"); } } int stack_pop() { if ( stack_idx > 0 ) { return stack[--stack_idx]; } else { fprintf(stderr, "stack is empty, returning -1.\n"); return -1; } } bool stack_empty() { return stack_idx > 0 ? 0 : 1; } bool stack_full() { return stack_idx < MAX ? 0 : 1; } int main ( int argc, char *argv[] ) { int i; char line[MAXLINE]; fprintf(stdout, "Please enter numbers to stacked:\n"); while ( fgets(line, sizeof(line), stdin) ) { i = atoi(line); if ( ! stack_full() ) { stack_push(i); } } while ( ! stack_empty() ) { fprintf(stdout, "%d\n", stack_pop()); } return 0; }