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

consider the following statements. stacktype stack; queuetype queue; int x, y;su

ID: 3868221 • Letter: C

Question

consider the following statements. stacktype stack; queuetype queue; int x, y;suppose that the input is 15 28 14 22 64 35 -19 32 7 11 13 30 -999

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

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 % 3)
{
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

stack.push(0);

queue.addQueue(0)

x = 15, case 0: stack.push(15)

x = 28, case 1:, Stack Element = 15 , stack.pop() Stack Element = 0,stack.pop()

x = 14, case 2: queue.addQueue(14)

x = 22, case 1: Sorry ,the stack is empty

x = 64, case 1: Sorry,the stack is empty

x = 35, case 2: queue.addQueue(35)

x = 19,case 1: Sorry,the stack is empty

x = 32, case 2, queue.addQueue(32)

x = 7, case 1,Sorry the stack is empty

x = 11,case 2, queue.addQueue(11)

x = 13,case 1, Sorry,the stack is empty

x = 30,case 0,stack.push(30)

Output:

Stack Element = 15

Stack.Element = 0

Sorry,the stack is empty

Sorry,the stack is empty

Sorry the stack is empty

Sorry,the stack is empty

Stack Element :

Stack.top : 30

Queue Elements: 0 14 35 32 11