/*-------------------------------------------------------------------*/ /* stack.c (Version 6: Generic ADTs) */ /*-------------------------------------------------------------------*/ #include "stack.h" #include #include struct Stack { void **ppvArray; int iTop; int iMaxSize; }; 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->ppvArray = (void**)calloc(iMaxSize, sizeof(void*)); assert(psStack->ppvArray != NULL); psStack->iTop = 0; psStack->iMaxSize = iMaxSize; return psStack; } void Stack_free(struct Stack *psStack) /* Free *psStack. */ { assert(psStack != NULL); free(psStack->ppvArray); free(psStack); } void Stack_push(struct Stack *psStack, void *pvItem) /* Push pvItem onto *psStack. */ { assert(psStack != NULL); assert(psStack->iTop < psStack->iMaxSize); (psStack->ppvArray)[psStack->iTop] = pvItem; ++(psStack->iTop); } void *Stack_pop(struct Stack *psStack) /* Pop *psStack, and return the popped item. */ { assert(psStack != NULL); assert(psStack->iTop > 0); --(psStack->iTop); return (psStack->ppvArray)[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; }