1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

*

we don't want functions outside of the module to be able to modify Stack, so we will define it in such a way that it can be stored in variables and passed a parameter, but it can't be manipulated

this can be done by making Stack a struct pointer, but not declaring the underlying struct, this can be done in the following way:

typedef struct stack_struct* Stack;

stack_struct isn't declared in stack.h, it is only declared in stack.c, so user functions can't manipulate stacks directly

now we come to stack.c, the file that contains the actual implementation of the stack module

First, we need to define the stack_struct type, for our simple implementation we will use an array to store the stack, since each stack could be a different size we will need to dynamically allocate the array and store its size in stack_struct

the declaration of stack_structure is:

*

*

*

*

*

typedef struct stack_struct {
int size;/* The size of the stack */
int top;
/* Index of the top of the stack */

} ;int* data;/* The actual stack data */

March 1, 1999

Page 13

C201/TAM