/*-------------------------------------------------------------------*/ /* stack.c (Version 3: Data Modules) */ /*-------------------------------------------------------------------*/ #include "stack.h" #include #include static double *pdStackArray; static int iStackTop; static int iStackMaxSize; void Stack_new(int iMaxSize) /* Create a new Stack that is able to store iMaxSize items, each of type double. */ { assert(iMaxSize > 0); pdStackArray = (double*)calloc(iMaxSize, sizeof(double)); assert(pdStackArray != NULL); iStackTop = 0; iStackMaxSize = iMaxSize; } void Stack_free() /* Free the Stack. */ { free(pdStackArray); } void Stack_push(double dItem) /* Push dItem onto the Stack. */ { assert(iStackTop < iStackMaxSize); pdStackArray[iStackTop] = dItem; ++iStackTop; } double Stack_pop() /* Pop the Stack, and return the popped item. */ { assert(iStackTop > 0); --iStackTop; return pdStackArray[iStackTop]; } int Stack_empty() /* Return 1 (TRUE) iff the Stack is empty. */ { return iStackTop == 0; } int Stack_full() /* Return 1 (TRUE) iff the Stack is full. */ { return iStackTop == iStackMaxSize; }