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

1-Consider the following statements: Stack stk; Queue que; int x; stk.push(0); q

ID: 3577673 • Letter: 1

Question

1-Consider the following statements:

Stack stk;                     Queue que;                 int x;

stk.push(0);

que.addQueue(0);

cin >> x;

while (x != -999)

{

            switch( x % 4)

{

case 0: stk.push(x);

break;

case 1:

if (!stk.isEmptyStack())

{

cout << “Stack Element = “ << stk.top();

stk.pop();

}//End if

else

cout << “Sorry, the stack is empty”);

break;

case 2: que.addQueue(x);

            break;

case3:

if (!que.isEmpty())

{

            cout << “Queue Element = “ << que.front();

            que.deleteQueue();

}//End If

else

cout <<”Sorry, the queue is empty”);

break;

}//End switch

cin >> x;

}//end While

cout <<”Stack Elements: “);

while (!stk.isEmptyStack()) {

cout << stk.top() << “ “;

stk.pop();

}

cout <<”Queue Elements: “);

while (!que.isEmptyQueue()) {

cout << que.front() << “ “;

que.deleteQueue();

}

Trace the code above –In addition to the output, show the state of the stack and queue for the following input: 15    28        14        22        64        35        19 -999.

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

2-

Trace the following statements (show your work). What is the output of this program?

stack s1, s2;

int list[]= {5, 10, 15, 20};

int i;

for (i = 0; i <= 3; i++)

            s1.push(list[i]);

while(!s1.isEmpty())

{

            s2.push(2* s1.top() – 3 * s1.top());

            s1.pop();

}

while (!s2.isEmpty())

{           cout << s2.top() << “ “;

            s2.pop();

}

Explanation / Answer

Here is the code traced:

Stack stk; Queue que; int x;
stk.push(0);           //0 will be pushed to stack. So, stack content is: 0.
que.addQueue(0);       //0 is added to queue. So, queue content is: 0.
cin >> x;               //Reads a value. Passed value is 15.
while (x != -999)       //And is not -999.
{
switch( x % 4)  
//For the first value, 15 % 4 is: 3.
//For the second value, 28 % 4 is: 0.
//For the third value, 14 % 4 is: 2.
//For the fourth value, 22 % 4 is: 2.
//For the fifth value, 64 % 4 is: 0.
//For the sixth value, 35 % 4 is: 3.
//For the seventh value, 19 % 4 is: 3.
{
case 0: stk.push(x);      
                   //So, 28 is pushed to stack.
                   //So, 64 is pushed to stack.
break;
case 1:
if (!stk.isEmptyStack())
{
cout << “Stack Element = “ << stk.top();
stk.pop();
}//End if
else
cout << “Sorry, the stack is empty”);
break;
case 2: que.addQueue(x);  
                       //So, 14 is added to queue.
                       //So, 22 is added to queue.
break;
case 3:
if (!que.isEmpty())           //Queue is not empty. //Queue is not empty. //Queue is not empty.
{
cout << "Queue Element = " << que.front(); //So, will print 0.   //So, will print 14. //So, will print 22.
que.deleteQueue();                           //And will be deleted. //And will be deleted. //And will be deleted.
}//End If
else
cout <<”Sorry, the queue is empty”);
break;
}//End switch
cin >> x;                  
//Reads a value. Passed value is 28.      
//Reads a value. Passed value is 14.
//Reads a value. Passed value is 22.
//Reads a value. Passed value is 64.
//Reads a value. Passed value is 35.
//Reads a value. Passed value is 19.
//Reads a value. Passed value is -999. And will come out of the loop.
}//end While
cout <<"Stack Elements: ");
while (!stk.isEmptyStack()) {
cout << stk.top() << “ “;
stk.pop();
}
cout <<"Queue Elements: ");
while (!que.isEmptyQueue()) {
cout << que.front() << “ “;
que.deleteQueue();
}
//15 28 14 22 64 35 19 -999.
//So, the output is:
//For value 15:
//Queue Element = 0
//Stack content is: 0.
//Queue content is: .
//For value 20:
//Stack content is: 28, 0.
//Queue content is: .
//For value 14:
//Stack content is: 28, 0.
//Queue content is: 14.
//For value 22:
//Stack content is: 28, 0.
//Queue content is: 14, 22.
//For value 64:
//Stack content is: 64, 28, 0.
//Queue content is: 14, 22.
//For value 19:
//Queue Element = 22
//Stack content is: 64, 28, 0.
//Queue content is: .

//And after the loop, the output is:
//Stack Elements: 64 28 0
//Queue Elements: