2 What would be the value of queues Q1 and Q2 and stack S af

2. What would be the value of queues Q1 and Q2, and stack S after the following algorithm segment:

1 S = createStack

2 Q1 = createQueue

3 Q2 = createQueue

4 enqueue (Q1, 5)

5 enqueue (Q1, 6)

6 enqueue (Q1, 9)

7 enqueue (Q1, 0)

8 enqueue (Q1, 7)

9 enqueue (Q1, 5)

10 enqueue (Q1, 0)

11 enqueue (Q1, 2)

12 enqueue (Q1, 6)

13 loop (not emptyQueue (Q1))

         1 dequeue (Q1, x)

         2 if (x == 0)

                    1 z=0

                  2 loop (not emptyStack (S))

                                  1 popStack (S, y)

                         2 z=z+y

                       3 end loop

                       4 enqueue (Q2, z)

         3 else
                       1 pushStack (S, x)

         4 end if

14 end loop

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

Solution

Please find below the execution results of this above algorithm :

After step 12 : Q1 = {5,6,9,0,7,5,0,2,6} // after step 12

dequeue Q1:    x = 5         after dequeue Q1 ={6,9,0,7,5,0,2,6}
       x != 0 ; thus push x into Stack S, thus S = {5}

dequeue Q1:    x = 6         after dequeue Q1 ={9,0,7,5,0,2,6}
       x != 0 ; thus push x into Stack S, thus S = {5,6}

dequeue Q1:    x = 9         after dequeue Q1 ={0,7,5,0,2,6}
       x != 0 ; thus push x into Stack S, thus S = {5,6,9}

dequeue Q1:    x = 0         after dequeue Q1 ={7,5,0,2,6}
       x == 0 ;thus let z = 0;
           pop stack S; y = 9 after pop S = {5,6}
           z = z + y = 0 + 9 = 9
             
           pop stack S; y = 6 after pop S = {5}
           z = 9 + 6 = 15

           pop stack S; y = 5 after pop S = {}
           z = 15 + 5 = 20
          
           stack S is empty; thus enqueue z into Q2; thus Q2 = {20}

dequeue Q1:    x = 7         after dequeue Q1 ={5,0,2,6}
       x != 0 ; thus push x into Stack S, thus S = {7}

dequeue Q1:    x = 5         after dequeue Q1 ={0,2,6}
       x != 0 ; thus push x into Stack S, thus S = {7,5}


dequeue Q1:    x = 0         after dequeue Q1 ={2,6}
       x == 0 ;thus let z = 0;
           pop stack S; y = 5 after pop S = {7}
           z = z + y = 0 + 5 = 5
             
           pop stack S; y = 7 after pop S = {}
           z = 5 + 7 = 12
          
           stack S is empty; thus enqueue z into Q2; thus Q2 = {20,12}


dequeue Q1:    x = 2         after dequeue Q1 ={6}
       x != 0 ; thus push x into Stack S, thus S = {2}


dequeue Q1:    x = 6         after dequeue Q1 ={}
       x != 0 ; thus push x into Stack S, thus S = {2,6}


Q1 is empty; thus exits loop.
  

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

From the above execution sequence we can find that the values in Q1, Q2, and S are :

Q1 = { } (empty)

Q2 = { 20, 12 }

S = { 2, 6 }

2. What would be the value of queues Q1 and Q2, and stack S after the following algorithm segment: 1 S = createStack 2 Q1 = createQueue 3 Q2 = createQueue 4 enq
2. What would be the value of queues Q1 and Q2, and stack S after the following algorithm segment: 1 S = createStack 2 Q1 = createQueue 3 Q2 = createQueue 4 enq

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site