CS 201 - 9/4/2014 /* actual C code */ #define TRUE 1 #define FALSE 0 #define EMPTYPOS 0 typedef struct StackStruct Stack; struct StackStruct { int arr[10]; int max; int top; }; --------------- int main () { Stack st1; Stack st2; /* initialize the stack variable instance */ /* inline init of st1 */ int i; st1.max = 10; st1.top = 0; for (i = 0 ; i < st1.max ; i++) st1.arr[i] = -999; /* function initial call */ stackInit (&st2); /* function call for isEmpty() */ if ( isEmpty(st1) == TRUE ) ..... while ( isEmpty(st2) == FALSE ) ...... push (&st1, 8); push (&st1, 15); push (&st1, 2); val = top (st1); pop (&st1); ---------------------------- /* code for stackInit() called from main */ void stackInit (Stack* pst) { int i; (*pst).max = 10; (*pst).top = 0; for (i = 0 ; i < (*pst).max ; i++) (*pst).arr[i] = -999; } /* alternitive syntax for the dereference syntax */ void stackInit (Stack* pst) { int i; pst->max = 10; pst->top = 0; for (i = 0 ; i < pst->max ; i++) pst->arr[i] = -999; } /* code for isEmpty() */ int isEmpty (Stack pst) { if ( pst.top == 0 ) return TRUE; else return FALSE; } int isEmpty (Stack pst) { return (pst.top == 0); } /* valid but less safe */ int isEmpty (Stack pst) { return !pst.top; } ---------------------------------- /* push - adds a value to the stack void push (Stack* pst, int val) { /* verify that room exists on the stack * if (pst->top == pst->max ) { /* error */ return ; } /* add the value to the top of the stack */ pst->arr[ pst->top ] = val; pst->top++; } int top (Stack pst) { return (pst.arr[pst.top-1]); } void pop (Stack* pst) { /* verify the stack is NOT empty */ if ( isEmpty(*pst) == TRUE) { /* error */ return; } pst->top--; pst->arr[pst->top] = -999; }