/* /* * stack.c - C version of integer stack module. * Stack.cpp - simple integer stack class. */ */ #include #include #include #include /* /* * Interface part - normally in header file. * Interface part - normally in header file. */ */ typedef struct class Stack { { int siz; public: int ndx; ~Stack (); int *rep; Stack (int size = 10); } Stack; void push (int val); int pull (); void stack_destroy (Stack *sp); int empty(); void stack_create (Stack *sp, int size); void stack_push (Stack *sp, int val); private: int stack_pull (Stack *sp); int siz; int stack_empty (Stack *sp); int ndx; int *rep; }; /* /* * Implementation part - normally in source file. * Implementation part - normally in source file. */ */ void stack_destroy(Stack *sp) Stack::~Stack() { { free(sp->rep); delete[siz] rep; } } void stack_create(Stack *sp, int size) Stack::Stack(int size) { { sp->siz = size; siz = size; sp->ndx = size; ndx = size; sp->rep = malloc(size * sizeof(int)); rep = new int[size]; } } void stack_push(Stack *sp, int val) void Stack::push(int val) { { if ( sp->ndx > 0 ) if ( ndx > 0 ) sp->rep[--sp->ndx] = val; rep[--ndx] = val; } } int stack_pull(Stack *sp) int Stack::pull() { { if ( sp->ndx < sp->siz) if ( ndx < siz) return sp->rep[sp->ndx++]; return rep[ndx++]; else else return 0; return 0; } } int stack_empty(Stack *sp) int Stack::empty() { { return sp->ndx == sp->siz; return ndx == siz; } } /* /* * Main program - normally in separate source file. * Main program - normally in separate source file. */ */ void main(int argc, char *argv[]) void main(int argc, char *argv[]) { { Stack stack; stack_create(&stack, argc - 1); Stack stack(argc - 1); while ( *++argv ) while ( *++argv ) stack_push( &stack, strtol(*argv, NULL, 0) ); stack.push( strtol(*argv, NULL, 0) ); while ( !stack_empty(&stack) ) while ( !stack.empty() ) printf("%d ", stack_pull(&stack) ); printf("%d ", stack.pull() ); stack_destroy(&stack); } } /*** End of file ***/ /*** End of file ***/