2. Imagine that you have two empty Queues Q1 and Q2, show the content of these q
ID: 3874600 • Letter: 2
Question
2. Imagine that you have two empty Queues Q1 and Q2, show the content of these queues after each of the following operations: Enqueue (Q1, 10) Enqueue (Q1, 20) Enqueue (Q1, 30) Enqueue (Q1, 40) Enqueue(02, 50) Enqueue(02, 60) Enqueue (Q1, 70) While (!isEmptyQueue (Q1)) X-Dequeue (Q2) Enqueue (Q1, X) 3. Imagine that the contents of Q1 and Q2 are shown below. What would be the content of Q3 after the following code is executed? The queue contens are shown front (left) and read (right) Q1: 42 30 41 31 19 20 25 14 1011 1215 Q2: 4 5 10 13 Q3-CreateQueue Count-0 Loop (not empty Q1 and not empty Q2) Count- count1 Dequeue (Q1, X) Dequeue (Q1, Y) if (y equal count) enqueue (Q3,X) End If End LoopExplanation / Answer
2)Two queues Q1,Q2
Enqueue(Q1,10)
now queue Q1 has elements 10
Enqueue(Q1,20)
now queue Q1 has elements 10,20
Enqueue(Q1,30)
now queue Q1 has elements 10,20,30
Enqueue(Q1,40)
now queue Q1 has elements 10,20,30,40
now insering 50 into Q2
Enqueue(Q2,50)
now queue Q1 has elements 50
Enqueue(Q2,60)
now queue Q1 has elements 60
now insering 70 into Q1
Enqueue(Q1,70)
now queue Q1 has elements 10,20,30,40,70
after the above operations contents of Q1 is 10,20,30,40,70 and Q2 is 50,60
now we have a while loop what it will do is for each iteration it will deletes one element from queue Q2 and inserts it into Q1 since we have two elements in Q2 it will insert 50 and 60 into Q1
sibce in queue we follow FIFO scheme after ibserting the content of Q1 will be
queue Q1 has elements 10,20,30,40,70,50,60
and Q2 will be empty
Q1:10 20 30 40 70 50 60
Q2:
in while loop the condition was !isEmptyQueue(Q1) is this i=was the condition in while it will result in infinite loop. I have given above explanation by assuming that that condition as !isEmptyQueue(Q2).
Then only loop will end otherwise it will be an infinite loop.
3)
Q1:42 30 41 3 19 20 25 14 10 11 122 15
Q2:4 5 10 13
Q3=createQueue //an empty queue Q3 has been created
count=0 //initialized count with zero
Loop(not empty Q1 and not empty Q2) //runs till both the queues are not empty if any one is empty loop terminates
count=count+1; //incrementing count
Dequeque(Q1,X); //deleteing one element form Q1 and storing in X
Dequeque(Q2,y); //deleteing one element form Q2 and storing in Y
if(Y equal count)//if element deleted from Q2 id equals to count then
enqueue(Q3,X);//inserting element deleted from Q1 into Q3 which was stored in X
End if
End Loop.
By executing above loop Q3 contents will be none which means that no element will be inserted into Q3
Explanation:
according to condition in loop it will execute for 4 times Q2 will become empty as it has only four elements and after that condition will fail and loop will be terminated.
first iteration:
x=42
y=4
count=1
y equal count false
so no insertion will be done
second iteration:
x=30
y=5
count=2
y equal count false
so no insertion will be done
first iteration:
x=41
y=10
count=3
y equal count false
so no insertion will be done
first iteration:
x=31
y=13
count=4
y equal count false
so no insertion will be done
Iteration 5 loop will be terminated as Q2 becomes empty
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.