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