Use the methods for stacks and queues developed in the course to write the following function: "Empty one Stack onto the top of another Stack in such a way that the entries that were in the first Stack are in the reverse of their original order". In writing your function, be sure to check for empty and full structures as appropriate. Your function may declare other, local structures as needed.


Difficulty level
Video recording
This exercise is mostly suitable for students
 void move(stack *s, stack *t)
 {
     queue q;
     element e;
     q=CreateQueue();
     
     while(Top(*s,&e) && EnQueue(&q,e) && Pop(s) );
     
     while(Front(q,&e) && Push (s,e) && DeQueue(&q));
     
     while(Top(*t,&e) && Push(s,e) && Pop(t) );
 }
 

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Implementation of an array using a stack