Algorithm 3 Queue Implemented using Two Stacks

Require: S1S_1 and S2S_2 are two stacks.

Ensure: Implementation of queue operations with S1S_1 and S2S_2 .

1:procedure Enqueue(S1,S2,xS_1, S_2, x)

2:Push(S2,xS_2, x)

3:end procedure

4:

5:procedure Dequeue(S1,S2S_1, S_2)

6:if StackEmpty(S1S_1) then

7:while not StackEmpty(S2S_2) do

8:x=x = Pop(S2S_2)

9:Push(S1,xS_1, x)

10:end while

11:end if

12:return Pop(S1S_1)

13:end procedure