0% found this document useful (0 votes)
3 views5 pages

Chapter7 Exercises - Solutions

Uploaded by

m25p8rghmp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Chapter7 Exercises - Solutions

Uploaded by

m25p8rghmp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXERCISES, CHAPTER 7

1. Consider the following statements:

stackType<int> stack;
int x, y;

Show what is output by the following segment of code:

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();

cout << "x = " << x << endl;


cout << "y = " << y << endl;

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

Show what is output by the following segment of code:


stack.push(5);
cin >> x;
while (x != -999)
{
if (x % 2 == 0)
{
if (!stack.isFullStack())
stack.push(x);
}
else{
cout << "x = " << x << endl;

cin >> x;
}

cout << "Stack Elements: ";

while (!stack.isEmptyStack())
{
cout << " " << stack.top();
stack.pop();
}

cout << endl;

3. Evaluate the following postfix expressions:

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 / + =

4. Convert the following infix expressions to postfix notations:


a. (A + B) * (C + D) - E
b. A - (B + C) * D + E / F
c. ((A + B) / (C - D) + E) * F - G
d. A + B * (C + D) - E / F * G + H
5. What is the output of the following program?

#include <iostream>
#include <string>
#include "myStack.h"
using namespace std;

void mystery(stackType<int>& s, stackType<int>& t);


int main()
{
int list[] = {5, 10, 15, 20, 25};

stackType<int> s1;
stackType<int> s2;

for (int i = 0; i < 5; i++)


s1.push(list[i]);

mystery(s1, s2);

while (!s2.isEmptyStack())
{
cout << s2.top() << " ";
s2.pop();
}
cout << endl;
}

void mystery(stackType<int>& s, stackType<int>& t)


{
while (!s.isEmptyStack())
{
t.push(2 * s.top());
s.pop();
}
}
6. What is the output of the following program segment?

linkedStackType<int> myStack;

myStack.push(10);
myStack.push(20);

myStack.pop();

cout << myStack.top() << endl;

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;

7. What is the output of the following program?


#include <iostream>
#include <string>
#include "myStack.h"

using namespace std;


template <class Type>

void mystery(stackType<Type>& s, stackType<Type>& t);

int main()
{
stackType<string> s1;
stackType<string> s2;

string list[] = {"Winter", "Spring", "Summer", "Fall",


"Cold", "Warm", "Hot"};

for (int i = 0; i < 7; i++)

s1.push(list[i]);

mystery(s1, s2);

while (!s2.isEmptyStack())
{
cout << s2.top() << " ";
s2.pop();
}
cout << endl;
}

template <class Type>

void mystery(stackType<Type>& s, stackType<Type>& t)


{
while (!s.isEmptyStack())
{
t.push(s.top());
s.pop();
}

}
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

7. Winter Spring Summer Fall Cold Warm Hot

You might also like