/* * ISA 563, Spring 2011 * Copyleft by Muhammad Abdulla */ #include #include #include "stack3.h" struct S { int stack[MAX]; int stack_idx; }; void stack_init(Stack *s) { s->stack_idx = 0; } void stack_push(Stack *s, int item) { if ( s->stack_idx < MAX ) { s->stack[(s->stack_idx)++] = item; } else { fprintf(stderr, "stack is full, returning.\n"); } } int stack_pop(Stack *s) { if ( s->stack_idx > 0 ) { return s->stack[--(s->stack_idx)]; } else { fprintf(stderr, "stack is empty, returning -1.\n"); return -1; } } int stack_empty(Stack *s) { return s->stack_idx > 0 ? 0 : 1; } int stack_full(Stack *s) { return s->stack_idx < MAX ? 0 : 1; }