The purpose of this question is to simulate the operation of a stack using 2 queues. Consider the following code:
typedef ... element ;
typedef struct {Queue L, R;} Stack ;
void Push(Stack *s, element e)
{
Enqueue(&(s->L),e) ;
}
Give the code for the Pop function by taking into account the previous code and knowing that the Queue R can be used to make temporary transfers of elements. Remember that this function should remove the most recently stacked element.
Difficulty level
Video recording
This exercise is mostly suitable for students
int Pop(stack *p)
{
element e1,e2;
if (isEmptyStack(*p)) return 0;
while(Front(p->L,&e1) && DeQueue(&(p->L)) && Front(p->L,&e2))
{
EnQueue(&(p->R),e1);
}
while(Front(p->R,&e1))
{
DeQueue(&(p->R));
EnQueue(&(p->L),e1);
}
return 1;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Farthest point from the circle center