Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following statements: stackType<int> stack; queueType<int> queue; i

ID: 3639875 • Letter: C

Question

Consider the following statements:
stackType<int> stack;
queueType<int> queue;
int x;

Suppose the input is
15 28 14 22 64 35 19 32 7 11 13 30 -999
Show what is written by the following segment of code:

stack.push(0);
queue.addQueue(0);
cin >> x;

while (x!= -999)
{
switch (x % 4)
{
case 0:
stack.push(x);
break;
case 1:
if (!stack.isEmptyStack())
{
cout << "Stack Element = " << stack.top()
<< endl;
stack.pop();
}
else
cout << "Sorry, the stack is empty." << endl;
break;
case 2:
queue.addQueue(x);
break;
case 3:
if (!queue.isEmptyQueue())
{
cout << "Queue Element = " << queue.front()
<< endl;
queue.deleteQueue();
}
else
cout << "Sorry, the queue is empty." << endl;
break;
} //end switch
cin >> x;
}
cout << "Stack Element: ";
while (!stack.isEmptyStack())
{
cout << stack.top() << " ";
stack.pop();
}
cout << endl;

cout << "Queue Elements: ";
while (!queue.isEmptyQueue())
{
cout << queue.front() << " " ;
queue.deleteQueue();
}
cout << endl;

Explanation / Answer

1.for x=15, (x%4)=3 therefore control shifts to case 3 since the queue is empty, the output will be "Sorry, the stack is empty." 2.for x=28, (x%4)=0 therefore control shifts to case 0 and a new stack element of 28 will be added to it 3. for x=14, (x%4)=2 therefore control shifts to case 2 and a new element 14 is added to the queue 4.for x=22, (x%4)=2 therefore control shifts to case 2 and an element 22 is added to the queue 5.for x=64, (x%4)=0 therefore control shifts to case 0 where a new element 65 is added to the stack 6. for x=35, (x%4)=3 therefore control shifts to case 3 where the output is displayed as "queue element=14" and the same element is also deleted from the queue 7. for x=19, (x%4)=3 therefore control shifts to case 3 where the output is displayed as "queue element =22" and the same element is also deleted from the queue 8. for x=32, (x%4)=0 therefore control shifts to case 0 where a new element 32 is added to the stack 9. for x=7, (x%4)=3 therefore control shifts to case 3 where the output will be displayed as "sorry queue is empty" 10. for x=11, (x%4)=3 therefore control shifts to case 3 where the output will be displayed as "sorry queue is empty" 11. for x=13, (x%4)=1 therefore control shifts to case 1 and the most element of the stack ie, 32 is displayed and then removed from the stack 12. for x=30, (x%4)=2 therefore control shifts to case 2 and hence a new element 30 is added to the queue 13. for x=-999 the while loop terminates as the condition returns to false hence the control skips the while statements and proceed towards the next codes first the stack gets emptied(following LIFO method) as follows the stack element: 64 28 then the queue gets emptied(following FIFO method) as follows the queue element: 30 the summarized output of the above code is as follows: " Sorry, the stack is empty. Queue Element =14 Queue Element =22 Sorry, the stack is empty. Sorry, the stack is empty. Stack Element =32 Stack Element: 64 28 Queue Elements: 30"