/* * implementation of integer stack * */ #include "intStack.h" #include #include #include boolean push(intStack *stack, int value) { if (stack->top > stack->max - 2) /* stack is full */ return FALSE; else { stack->top++; stack->valueList[stack->top] = value; return TRUE; } } int pop(intStack *stack) { assert(!empty(stack)); /* crash informatively otherwise */ return stack->valueList[stack->top--]; /* evaluate, then decrement */ } int peek(intStack *stack) { assert(!empty(stack)); /* crash informatively otherwise */ return stack->valueList[stack->top]; /* no decrement */ } boolean initialize(intStack *stack, int capacity) { stack->valueList = (int *) malloc(capacity * sizeof(int)); if (stack->valueList == NULL) return FALSE; else { stack->top = -1; /* empty stack */ stack->max = capacity; return TRUE; } } boolean destroy(intStack *stack) { if (stack->valueList) { /* it's non-NULL */ free (stack->valueList); stack->valueList = NULL; stack->top = -1; stack->max = 0; return TRUE; } else { return FALSE; } } int count(intStack* stack) { return stack->top + 1; } boolean empty(intStack *stack) { return stack->top < 0; } boolean full(intStack *stack) { return stack->top > stack->max - 2; } void printStack(intStack *stack) { int i; /* loop index */ for (i = 0; i <= stack->top; i++) { printf("%d ", stack->valueList[i]); } }