/*------------------------------------------------------------------*/ /* 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; /*------------------------------------------------------------------*/ void Stack_init(void) /* Initialize the Stack. It is a checked runtime error for the Stack to be initialized. */ { assert(! iInitialized); iTop = 0; iInitialized = 1; } /*------------------------------------------------------------------*/ void Stack_free(void) /* Free the resources consumed by the Stack. It is a checked runtime error for the Stack to be uninitialized. */ { assert(iInitialized); iInitialized = 0; } /*------------------------------------------------------------------*/ void Stack_push(int iItem) /* Push iItem onto the Stack. It is a checked runtime error for the Stack to uninitialized or to be full. */ { assert(iInitialized); 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 be uninitialized or to be empty. */ { assert(iInitialized); assert(! Stack_isEmpty()); iTop--; return piContents[iTop]; } /*------------------------------------------------------------------*/ int Stack_isEmpty(void) /* Return 1 (TRUE) iff the Stack is empty. It is a checked runtime error for the Stack to be uninitialized. */ { assert(iInitialized); return iTop == 0; } /*------------------------------------------------------------------*/ int Stack_isFull(void) /* Return 1 (TRUE) iff the Stack is full. It is a checked runtime error for the Stack to be uninitialized. */ { assert(iInitialized); return iTop == MAX_STACK_SIZE; }