/* * ISA 563, Spring 2011 * Copyleft by Muhammad Abdulla */ #include #include #include #include "stack4.h" #define S Stack #define MAX 100 struct S { int stack[MAX]; int stack_idx; }; S stack_new() { S s; s = ( S ) malloc ( sizeof ( *s ) ) ; assert(s != NULL); s->stack_idx = 0; return s; } void stack_push(S 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(S 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(S s) { return s->stack_idx > 0 ? 0 : 1; } int stack_full(S s) { return s->stack_idx < MAX ? 0 : 1; } void stack_free(S s) { if ( ! s ) { return; } free(s); }