Chapter7 Exercises - Solutions
Chapter7 Exercises - Solutions
stackType<int> stack;
int x, y;
x = 4; y = 0;
stack.push(7);
stack.push(x);
stack.push(x + 5);
y = stack.top();
stack.pop();
stack.push(x + y);
stack.push(y - 2);
stack.push(3);
x = stack.top();
stack.pop();
while (!stack.isEmptyStack())
{
cout << stack.top() << endl;
stack.pop();
}
2. Consider the following statements:
stackType<int> stack;
int x;
Suppose that the input is:
14 45 34 23 10 5 -999
cin >> x;
}
while (!stack.isEmptyStack())
{
cout << " " << stack.top();
stack.pop();
}
a. 8 2 + 3 * 16 4 / - =
b. 12 25 5 1 / / * 8 7 + - =
c. 70 14 4 5 15 3 / * - - / 6 + =
d. 3 5 6 * + 13 - 18 2 / + =
#include <iostream>
#include <string>
#include "myStack.h"
using namespace std;
stackType<int> s1;
stackType<int> s2;
mystery(s1, s2);
while (!s2.isEmptyStack())
{
cout << s2.top() << " ";
s2.pop();
}
cout << endl;
}
linkedStackType<int> myStack;
myStack.push(10);
myStack.push(20);
myStack.pop();
myStack.push(25);
myStack.push(2 * myStack.top());
myStack.push(-10);
myStack.pop();
linkedStackType<int> tempStack;
tempStack = myStack;
while (!tempStack.isEmptyStack())
{
cout << tempStack.top() << " ";
tempStack.pop();
}
cout << endl;
cout << myStack.top() << endl;
int main()
{
stackType<string> s1;
stackType<string> s2;
s1.push(list[i]);
mystery(s1, s2);
while (!s2.isEmptyStack())
{
cout << s2.top() << " ";
s2.pop();
}
cout << endl;
}
}
SOLUTIONS
1.
x = 3
y = 9
7
13
4
7
2.
x = 45
x = 23
x = 5
Stack Elements: 10 34 14 5
3.
a. 26
b. 45
c. 8
d. 29
4.
a. AB+CD+*E-
b. ABC+D*-EF/+
c. AB+CD-/E+F*G-
d. ABCD+*+EF/G*-H+
5. 10 20 30 40 50
6. 10
50 25 10
50