What would be the contents of queue Q1 and queue Q2 after th

What would be the contents of queue Q1 and queue Q2 after the following code is executed and the following data are entered?

1 Q1 = createQueue
2 Q2 = createQueue
3 loop (not end of file)

      1.read number
      2.enqueue (Q1, number)
      3.enqueue (Q2, number)
      4.loop (not empty Q1)

           1 dequeue (Q1, x)

           2 enqueue (Q2, x)

       5.end loop

4 end loop

The data are 5, 7, 12, 4, 0, 4, 6.

This is a C data structure question. Please use C language, thank you very much!

Solution

please see below the execution sequences for the given algorithm :

The data are 5, 7, 12, 4, 0, 4, 6.

1 Q1 = createQueue
2 Q2 = createQueue
3 loop (not end of file)
1.read number
2.enqueue (Q1, number)
3.enqueue (Q2, number)
4.loop (not empty Q1)
1 dequeue (Q1, x)
2 enqueue (Q2, x)
5.end loop
4 end loop
------------------------------

created Q1 = {}
created Q2 = {}

loop till end of file is encountered :
  
   read number = 5
   enqueue Q1; Q1 = {5}
   enqueue Q2; Q2 = {5}

   loop till Q1 is empty :

       dequeue Q1; thus Q1 = { }
       enqueue Q2; thus Q2 = { 5 , 5}
  
   inner loop exits as Q1 is empty.

   read number = 7
   enqueue Q1; Q1 = {7}
   enqueue Q2; Q2 = {5,5,7}

   loop till Q1 is empty :

       dequeue Q1; thus Q1 = { }
       enqueue Q2; thus Q2 = { 5 , 5, 7, 7}
  
   inner loop exits as Q1 is empty.

   read number = 12
   enqueue Q1; Q1 = {12}
   enqueue Q2; Q2 = {5,5,7,7,12}

   loop till Q1 is empty :

       dequeue Q1; thus Q1 = {}
       enqueue Q2; thus Q2 = {5,5,7,7,12,12}
  
   inner loop exits as Q1 is empty.

   read number = 4
   enqueue Q1; Q1 = {4}
   enqueue Q2; Q2 = {5,5,7,7,12,12,4}

   loop till Q1 is empty :

       dequeue Q1; thus Q1 = {}
       enqueue Q2; thus Q2 = {5,5,7,7,12,12,4,4}
  
   inner loop exits as Q1 is empty.

   read number = 0
   enqueue Q1; Q1 = {0}
   enqueue Q2; Q2 = {5,5,7,7,12,12,4,4,0}

   loop till Q1 is empty :

       dequeue Q1; thus Q1 = {}
       enqueue Q2; thus Q2 = {5,5,7,7,12,12,4,4,0,0}
  
   inner loop exits as Q1 is empty.

   read number = 4
   enqueue Q1; Q1 = {4}
   enqueue Q2; Q2 = {5,5,7,7,12,12,4,4,0,0,4}

   loop till Q1 is empty :

       dequeue Q1; thus Q1 = {}
       enqueue Q2; thus Q2 = {5,5,7,7,12,12,4,4,0,0,4,4}
  
   inner loop exits as Q1 is empty.

   read number = 6
   enqueue Q1; Q1 = {4}
   enqueue Q2; Q2 = {5,5,7,7,12,12,4,4,0,0,4,6}

   loop till Q1 is empty :

       dequeue Q1; thus Q1 = {}
       enqueue Q2; thus Q2 = {5,5,7,7,12,12,4,4,0,0,4,4,6,6}
  
   inner loop exits as Q1 is empty.

   read number fails as it reached end of file.
outer loop exits as EOF reached.

----------------------------------------------

Thus after the execution of the whole algorithm, the contents of Q1 and Q2 are :

Q1 : { } (empty)

Q2 : {5,5,7,7,12,12,4,4,0,0,4,4,6,6}

What would be the contents of queue Q1 and queue Q2 after the following code is executed and the following data are entered? 1 Q1 = createQueue 2 Q2 = createQue
What would be the contents of queue Q1 and queue Q2 after the following code is executed and the following data are entered? 1 Q1 = createQueue 2 Q2 = createQue
What would be the contents of queue Q1 and queue Q2 after the following code is executed and the following data are entered? 1 Q1 = createQueue 2 Q2 = createQue

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site