/*------------------------------------------------------------------*/ /* stackao.c: A Stack Abstract Object */ /*------------------------------------------------------------------*/ #include #include #include "stackao.h" #define MAX_STACK_SIZE 100 /*------------------------------------------------------------------*/ /* The state of the Stack */ static int iInitialized = 0; static int piContents[MAX_STACK_SIZE]; static int iTop; /*------------------------------------------------------------------*/ static int Stack_isValid(void) { return iInitialized; } /*------------------------------------------------------------------*/ void Stack_init(void) /* Initialize the Stack. */ { assert(! Stack_isValid()); iTop = 0; iInitialized = 1; } /*------------------------------------------------------------------*/ void Stack_free(void) /* Free the resources consumed by the Stack. */ { assert(Stack_isValid()); iInitialized = 0; } /*------------------------------------------------------------------*/ void Stack_push(int iItem) /* Push iItem onto the Stack. It is a checked runtime error for oStack to be full. */ { assert(Stack_isValid()); assert(! Stack_isFull()); piContents[iTop] = iItem; iTop++; } /*------------------------------------------------------------------*/ int Stack_pop(void) /* Pop the Stack, and return the popped item. It is a checked runtime error for the Stack to empty. */ { assert(Stack_isValid()); assert(! Stack_isEmpty()); iTop--; return piContents[iTop]; } /*------------------------------------------------------------------*/ int Stack_isEmpty(void) /* Return 1 (TRUE) iff the Stack is empty. */ { assert(Stack_isValid()); return iTop == 0; } /*------------------------------------------------------------------*/ int Stack_isFull(void) /* Return 1 (TRUE) iff the Stack is full. */ { assert(Stack_isValid()); return iTop == MAX_STACK_SIZE; }