Code:
T stack1[SIZE];
T stack2[SIZE];
T stack3[SIZE];
T stack4[SIZE];
Any questions?
A well designed stack will allow for multiple simultaneous instances within the program. For example.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct stack xstack;
struct stack {
int *base;
int size;
int top;
};
xstack *make_xstack(int size);
void destroy_xstack(xstack *stack);
int empty_xstack(xstack *stack);
int top_xstack(xstack *stack);
void push_xstack(xstack *stack, int item);
void pop_xstack(xstack *stack);
int main(void)
{
xstack *stack1;
xstack *stack2;
int i;
stack1 = make_xstack(10);
stack2 = make_xstack(10);
for (i = 0; i < 10; i++) {
push_xstack(stack1, i);
push_xstack(stack2, i * i);
}
while (!empty_xstack(stack1)) {
printf("%d : %d\n", top_xstack(stack1), top_xstack(stack2));
pop_xstack(stack1);
pop_xstack(stack2);
}
destroy_xstack(stack1);
destroy_xstack(stack2);
return 0;
}
Because each function works on a pointer to xstack passed as an argument, you don't need to use any global or stack data and multiple stacks can be maintained at any given time.