/*------------------------------------------------------------------*/ /* stackadt.c: A Stack ADT */ /*------------------------------------------------------------------*/ #include #include #include "stackadt.h" #define MAX_STACK_SIZE 100 struct Stack { int piContents[MAX_STACK_SIZE]; int iTop; }; /*------------------------------------------------------------------*/ Stack_T Stack_new(void) /* Return a new Stack_T. */ { Stack_T oStack; oStack = (Stack_T)malloc(sizeof(*oStack)); assert(oStack != NULL); oStack->iTop = 0; return oStack; } /*------------------------------------------------------------------*/ void Stack_free(Stack_T oStack) /* Free oStack. */ { if (oStack == NULL) return; free(oStack); } /*------------------------------------------------------------------*/ void Stack_push(Stack_T oStack, int iItem) /* Push iItem onto oStack. It is a checked runtime error for oStack to be NULL or full. */ { assert(oStack != NULL); assert(! Stack_isFull(oStack)); oStack->piContents[oStack->iTop] = iItem; oStack->iTop++; } /*------------------------------------------------------------------*/ int Stack_pop(Stack_T oStack) /* Pop oStack, and return the popped item. It is a checked runtime error for oStack to be NULL or empty. */ { assert(oStack != NULL); assert(! Stack_isEmpty(oStack)); oStack->iTop--; return oStack->piContents[oStack->iTop]; } /*------------------------------------------------------------------*/ int Stack_isEmpty(Stack_T oStack) /* Return 1 (TRUE) iff oStack is empty. It is a checked runtime error for oStack to be NULL. */ { assert(oStack != NULL); return oStack->iTop == 0; } /*------------------------------------------------------------------*/ int Stack_isFull(Stack_T oStack) /* Return 1 (TRUE) iff oStack is full. It is a checked runtime error for oStack to be NULL. */ { assert(oStack != NULL); return oStack->iTop == MAX_STACK_SIZE; }