[prev] 17 [next]

Stack as ADO (cont)

Implementation (in a file named Stack.c):

#include "Stack.h"
#include <assert.h>

#define MAXITEMS 10
static struct {
   char item[MAXITEMS];
   int  top;
} stackObject;  // defines the Data Object

// set up empty stack
void StackInit() {
   stackObject.top = -1;
}

// check whether stack is empty
int StackIsEmpty() {
   return (stackObject.top < 0);
}



// insert char on top of stack
void StackPush(char ch) {
   assert(stackObject.top < MAXITEMS-1);
   stackObject.top++;
   int i = stackObject.top;
   stackObject.item[i] = ch;
}

// remove char from top of stack
char StackPop() {
   assert(stackObject.top > -1);
   int i = stackObject.top;
   char ch = stackObject.item[i];
   stackObject.top--;
   return ch;
}

assert(test) terminates program with error message if test fails.