#include /* Stack ADT Type Defintions Source: G & F */ struct intstack { int *stackAry; int count; int stackMax; int top; } ; /* =============== createStack ============== */ /* This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow. */ static struct intstack *createStack (int maxElements) { /* Local Declarations */ struct intstack *stack; stack = (struct intstack *) malloc( sizeof (struct intstack) ) ; if (stack == NULL) return NULL ; /* Head Allocated. Now initialize and allocate stack. */ stack->top = -1; stack->count = 0; stack->stackMax = maxElements; stack->stackAry = (int *) calloc(stack->stackMax, sizeof(int)); if(stack->stackAry == NULL) { /* free all allocated space if no more memory */ free(stack); return NULL; } return stack ; } /* createStack */ /* =============== pushStack ============== */ /* This function pushes an item onto the stack. Pre stack is a pointer to the stack dataIn is data to be inserted Post Returns 1 if success; 0 if overflow */ static int pushStack(struct intstack *stack, int dataIn) { if (stack->count == stack->stackMax) return 0; (stack->count)++; (stack->top)++; stack->stackAry[stack->top] = dataIn; return 1; } /* pushStack */ /* =============== popStack ============== */ /* This function pops the item on the top of the stack. Pre stack is a pointer to the stack Post Returns 1 if success; 0 if underflow dataPtr is a pointer to the data popped */ static int popStack (struct intstack *stack, int *dataOutPtr) { if (stack->count == 0) return 0; else { *dataOutPtr = stack->stackAry[stack->top]; (stack->count)--; (stack->top)--; return 1; } /* else */ } /* popStack */ /* =============== stackTop ============== */ /* This function retrieves the data from the top of the stack without changing the stack. Pre stack is a pointer to the stack Post Returns 1 if success; 0 if underflow dataPtr is a pointer to the data on the top */ static int stackTop (struct intstack *stack, int *dataOutPtr) { if (stack->count > 0) { *dataOutPtr = stack->stackAry[stack->top]; return 1; } else return 0; } /* stackTop */ /* =============== emptyStack ============== */ /* This function determines if a stack is empty Pre stack is a pointer to the stack Post returns 1 if empty; 0 if data are in the stack */ static int emptyStack (struct intstack *stack) { return (stack->count == 0); } /* emptyStack */ /* =============== fullStack ============== */ /* This function determines if a stack is full. Full is defined as heap full Pre stack is a pointer to a stack head node Post returns 1 if heap is full; 0 if heap has room */ static int fullStack (struct intstack *stack) { /* Statements */ return (stack->count == stack->stackMax); } /* fullStack */ /* =============== stackCount ============== */ /* This function returns the number of elements in the stack. Pre stack is a pointer to the stack Post count returned */ static int stackCount(struct intstack *stack) { /* Statements */ return stack->count; } /* stackCount */ /* =============== destroyStack ============== */ /* This function releases all nodes to the heap. Pre A stack Post returns null pointer. */ static struct intstack *destroyStack ( struct intstack *stack ) { /* Statements */ if (stack) { /* Release stack array */ free(stack->stackAry); /* Release stack head */ free(stack); } /* if stack */ return NULL; } /* destroyStack */