Suppose that q1 and q2 are two different queues and that at
Suppose that q1 and q2 are two different queues, and that, at the moment, they both contain integer values as shown here:
Show the contents of both q1 and q2 after executing the following code. You may wish to draw their state after each line of code.
q1.pop();
q1.push( q1.front() );
q2.pop();
q1.push( q2.front() - 1 );
q1.pop();
q2.push( q2.front() / q1.front() );
front rear front rear q2 q2Solution
q1.pop() = 3 as front is at 3 now front moves to 2
q1 = 2 1
q1.push(q1.front()) = q1.push( 2 )
q1: 2 2 1
q2.pop() = 4 as front is at 4 ,now front is at 5
q2 :5 6
q1.push(q2.front() -1) = q1.push(5-1)= q1.push(4)
q1 : 4 2 2 1
q1.pop()=2 ,now front at 1
q1 : 4 2 1
q2.push(q2.front()/q1.front())= q2.push( 5/1)=q2.push(5)
q2 : 5 5 6
Finally
q1 : 4 2 1 front at 1 rear at 4
q2 : 5 5 6 front at middle 5 and rear at the initial 5
