Can anyone help me with the step-by-step output to the following stack, I\'m con
ID: 3833022 • Letter: C
Question
Can anyone help me with the step-by-step output to the following stack, I'm confused with the concept. Like what is in the stack after operation stack.push(7), stack.push(4) and so on...
Stack<int> stack;
int x, y, temp;
x = 4;
y = 0;
stack.push(7);
stack.push(x);
stack.push(x + 5);
stack.pop(y);
stack.push(x + y);
stack.push(y - 2);
stack.push(3);
stack.pop(x);
cout << " x: " << x << " y: " << y << endl;
while(!stack.empty())
{
stack.pop(temp);
cout << temp << setw(5);
}
Explanation / Answer
int x, y, temp;
x = 4;
y = 0;
stack.push(7); // top -> 7
stack.push(x); // top -> 4 7
stack.push(x + 5); //top -> 9 4 7
stack.pop(y); //top -> 4 7
now, y = 9
stack.push(x + y); //top -> 13 4 7
stack.push(y - 2); //top -> 7 13 9 4 7
stack.push(3); //top -> 3 7 13 9 4 7
stack.pop(x); //top -> 7 13 9 4 7
now, x = 3
cout << " x: " << x << " y: " << y << endl; // output: x: 3 y: 9
while(!stack.empty())
{
stack.pop(temp);
cout << temp << setw(5);
}
output of while loop:
7 13 9 4 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.