Explain what does the following function do void fooQueue q
Explain what does the following function do void foo(Queue *q, Stack *s) { while (!q rightarrow isEmpty() { s rightarrow push(q rightarrow dequeue); } while(!s.isEmpty()) { q rightarrow enqueue(s, pop()); } } Explain what does the following function
Solution
Here q, s are two different pointers, and of user defined type queue and stack respectively,
1. while(! q->isEmpty) // which means queue is not empty i.e still elements are in the queue
{ s->push(q->dequeue) } which means delete the element from the queue and push in to stack which will be taken by the pointer s.
2. while(!s.isEmpty()) // which means still elements are in the stack unless stack is empty
{
q->enqueue(s,pop()); delete the elements from the stack and insert into the queue..
}
ultimately , which results exchanging the elements elements from stack to queue and vice-versa
