Well, in C, which is pretty much like in C++, you can code stack and queue using array.

stack:

- Code: Select all
`#define MAX 100`

int stack[MAX];

int top = 0; // first empty place

// add an element

int push (int x) {

if (top == MAX) return 0; // stack is full, operation failed

stack[top++] = x;

return 1; // operation succeeded

}

// take an element

int pop () {

int x;

if (top == 0) return -1; // stack is empty, operation failed (return -1, or any other negative number,

// or 0, if stack contains natural numbers)

x = stack[--top];

return x;

}

// read the top, without removing the element

int read() {

int x;

if (top == 0) return -1; // stack is empty, operation failed

x = stack[top];

return x;

}

// is stack empty

int empty() {

if (top == 0) return 1;

else return 0;

}

// is stack full

int full() {

if (top == MAX) return 1;

else return 0;

}

You can code these five functions to be unique for all stacks, if you have more of them. You just need one more argument, which stack you are referring to.

- Code: Select all
`#define MAX 100`

typedef struct stack {

int elements[MAX];

int top;

} Stack;

Stack stack1, stack2, stack3, stack4, stack5;

stack1.top = 0; stack2.top = 0; stack3.top = 0; stack4.top = 0; stack5.top = 0;

// add an element

int push (stack1, int x) {

if (stack1.top == MAX) return 0; // stack is full, operation failed

stack1.elements[stack1.top++] = x;

return 1; // operation succeeded

}

// take an element

int pop (stack2) {

int x;

if (stack2.top == 0) return -1; // stack is empty, operation failed (return -1, or any other negative number,

// or 0, if stack contains natural numbers)

x = stack2.elements[--stack2.top];

return x;

}

// read the top, without removing the element

int read(stack3) {

int x;

if (stack3.top == 0) return -1; // stack is empty, operation failed

x = stack3.elements[stack3.top];

return x;

}

// is stack empty

int empty(stack4) {

if (stack3.top == 0) return 1;

else return 0;

}

// is stack full

int full(stack5) {

if (stack4.top == MAX) return 1;

else return 0;

}

queue:

- Code: Select all
`#define MAX 100`

int queue[MAX];

int top = 0; // here elements exit queue

int bottom = 0; // here elements enter queue

int N = 0; // number of elements

// insert an element

int insert(int x) {

if (N == MAX) return 0; // queue is full, operation failed

bottom = (bottom + 1) % MAX; // set bottom to next empty element

queue[bottom] = x;

N++;

return 1; // operation succeeded

}

// take an element

int take() {

int x;

if (N == 0) return -1; // queue is empty, operation failed

top = (top + 1) % MAX;

x = queue[top];

N--;

return x;

}

// is queue empty

int empty() {

if (N == 0) return 1;

else return 0;

}

// is queue full

int full() {

if (N == MAX) return 1;

else return 0;

}

You can also make these functions universal, to work with all queues of this format.