stackElt bottom(stack s) { // s is not empty
return(s.elts[0]);
}
// auxiliary functions that are convenient in the implementation of some queue functions
void showStackTopToBottom(stack s) {
int n = sizeOfStack(s);
while (n)
printf("%2c", s.elts[--n]);
}
void showStackBottomToTop(stack s) {
int n = sizeOfStack(s),
i = 0;
while (i < n)
printf("%2c", s.elts[i++]);
}
/* DEFINITION OF THE QUEUE FUNCTIONS */
queue createQueue() {
queue q;
q.in = createStack();
q.out = createStack();
return(q);
}
bool isEmptyQueue(queue q) {return(isEmptyStack(q.in) && isEmptyStack(q.out));}
bool isFullQueue (queue q) {return(isFullStack(q.in));}
int sizeOfQueue (queue q) {return(q.in.size + q.out.size);}
queue insert(queueElt e, queue q) { // q is not full
q.in = push(e, q.in);
return(q);
}
queue rearrangeQueue(queue q) {
while (!isEmptyStack(q.in)) {
q.out = push(top(q.in), q.out);
q.in = pop(q.in);
}
return(q);
}
queue serve(queue q) { // q is not empty
if (isEmptyStack(q.out))
q = rearrangeQueue(q);
q.out = pop(q.out);
return(q);
}
queueElt front(queue q) { // q is not empty
return((isEmptyStack(q.out))
? bottom(q.in)
: top(q.out));
}
queueElt rear(queue q) { // q is not empty
return(top(q.in));
}
void showQueueFrontToRear(queue q) {
printf("[");
printf("["); showStackTopToBottom(q.out); printf("]");
printf("["); showStackBottomToTop(q.in); printf("]");
printf("]\n");
}