• Home
  • Textbooks
  • C++ Programming: Program Design Including Data Structures
  • Stacks and Queues

C++ Programming: Program Design Including Data Structures

D. S. Malik

Chapter 17

Stacks and Queues - all with Video Answers

Educators


Chapter Questions

01:59

Problem 1

Describe the two basic operations on a stack.

Adam Conner
Adam Conner
Numerade Educator
02:39

Problem 2

Suppose that stack is an object of type stackType<int>. What is the difference between stack.top and stack.top - 1?

SS
Sarvesh Somasundaram
Numerade Educator
02:39

Problem 3

Suppose that stack is an object of type stackType<double> and the value of stack.top is 5. What is the index of the top element of the stack?

SS
Sarvesh Somasundaram
Numerade Educator
02:39

Problem 4

Suppose that stack is an object of type stackType<string> and the value of stack.top - 1 is 2. How many elements are in the stack?

SS
Sarvesh Somasundaram
Numerade Educator
01:15

Problem 5

Consider the following statements:
stackType<int> stack;
int num1, num2;
Show what is output by the following segment of code:
stack.push(12);
stack.push(5);
num1 = stack.top() + 3;
stack.push(num1 + 5);
num2 = stack.top();
stack.push(num1 + num2);
num2 = stack.top();
stack.pop();
stack.push(15);
num1 = stack.top();
stack.pop();
while (!stack.isEmptyStack())
{
cout << stack.top() << " ";
stack.pop();
}
cout << endl;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;

Willis James
Willis James
Numerade Educator
02:39

Problem 6

Consider the following statements:
stackType<int> stack(50);
int num;
Suppose that the input is:
31 47 86 39 62 71 15 63
Show what is output by the following segment of code:
cin >> num;
while (cin)
{
if (!stack.isFullStack())
{
if (num % 2 == 0 || num % 3 == 0)
stack.push(num);
else if (!stack.isEmptyStack())
{
cout << stack.top() << " ";
stack.pop();
}
else
stack.push(num + 3);
}
cin >> num;
}
cout << endl;
cout << "Stack Elements: ";
while (!stack.isEmptyStack())
{
cout << " " << stack.top();
stack.pop();
}
cout << endl;

SS
Sarvesh Somasundaram
Numerade Educator
05:24

Problem 7

Evaluate the following postfix expressions:
a. $1753-* 6+=$
b. $142 * 8+6 / 5+=$
c. $15163102++-+8 /=$
d. $1812--9+255 / \star=$

Chris Trentman
Chris Trentman
Numerade Educator
16:43

Problem 8

Convert the following infix expressions to postfix notations:
a. x * (y + z) - ( w + t)
b. (x + y) / (z - w) * t
c. ((x - y) + (z - w) / t) * u
d. x - y / (z + w) * t / u + (v - s)

Chris Trentman
Chris Trentman
Numerade Educator
16:43

Problem 9

Write the equivalent infix expressions for the following postfix expressions:
a. x y + z * w -
b. x y * z / w +
c. x y z + * w -

Chris Trentman
Chris Trentman
Numerade Educator
02:34

Problem 10

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>& s1, stackType<type>& s2,
stackType<type>& s3);
int main()
{
stackType<string> stack1;
stackType<string> stack2;
stackType<string> newStack;
string fNames[] = {"Chelsea", "Kirk", "David", "Stephanie",
"Bianca", "Katie", "Holly"};
string lNames[] = {"Jackson", "McCarthy", "Miller", "Pratt",
"Hollman", "Smith", "Klien"};
for (int i = 0; i < 7; i++)
{
stack1.push(fNames[i]);
stack2.push(lNames[i]);
}
mystery(stack1, stack2, newStack);
while (!newStack.isEmptyStack())
{
cout << newStack.top() << endl;
newStack.pop();
}
}
template <class type>
void mystery(stackType<type>& s1, stackType<type>& s2,
stackType<type>& s3)
{
while (!s1.isEmptyStack() && !s2.isEmptyStack())
{
s3.push(s1.top() + " " + s2.top());
s1.pop();
s2.pop();
}
}

Ernest Castorena
Ernest Castorena
Numerade Educator
02:22

Problem 11

What is the output of the following program?
#include <iostream>
#include <cmath>
#include "myStack.h"
using namespace std;
void mystery(stackType<int>& s, stackType<double>& t);
int main()
{
int list[] = {1, 2, 3, 4, 5};
stackType<int> s1;
stackType<double> 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<double>& t)
{
double x = 1.0;
while (!s.isEmptyStack())
{
t.push(pow(s.top(), x));
s.pop();
x = x + 1;
}
}

Kumareshwaran Rathinasabapathy
Kumareshwaran Rathinasabapathy
Numerade Educator
02:54

Problem 12

Explain why, in the linked implementation of a stack, it is not necessary to implement the operation to determine whether the stack is full.

SS
Sarvesh Somasundaram
Numerade Educator
02:39

Problem 13

Suppose that stack is an object of type linkedStackType<int>. What is the difference between the statements stack.top(); and stack.pop();?

SS
Sarvesh Somasundaram
Numerade Educator
03:36

Problem 14

Write the definition of the function template printListReverse that uses a stack to print a linked list in reverse order. Assume that this function is a member of the class linkedListType, designed in Chapter 16.

Tarandeep Singh
Tarandeep Singh
Numerade Educator
01:54

Problem 15

Write the definition of the method second that takes as a parameter a stack object and returns the second element of the stack. The original stack remains unchanged.

SS
Sarvesh Somasundaram
Numerade Educator
View

Problem 16

Consider the following statements:
queueType<int> queue;
int num;
Show what is output by the following segment of code:
num = 7;
queue.addQueue(6);
queue.addQueue(num);
num = queue.front();
queue.deleteQueue();
queue.addQueue(num + 5);
queue.addQueue(14);
queue.addQueue(num - 2);
queue.addQueue(25);
queue.deleteQueue();
cout << "Queue elements: ";
while (!queue.isEmptyQueue())
{
cout << queue.front() << " ";
queue.deleteQueue();
}
cout << endl;

Gio Maya
Gio Maya
Numerade Educator
04:03

Problem 17

Consider the following statements:
linkedStackType<int> stack;
linkedQueueType<int> queue;
int num;
Suppose the input is:
48 35 72 88 92 11 10 15 44 52 67 36
Show what is written by the following segment of code:
stack.push(0);
queue.addQueue(0);
cin >> num;
while (cin)
{
switch (num % 3)
{
case 0:
stack.push(num);
break;
case 1:
queue.addQueue(num);
break;
case 2:
if (!stack.isEmptyStack())
{
cout << stack.top() << " ";
stack.pop();
}
else if (!queue.isEmptyQueue())
{
cout << queue.front() << " ";
queue.deleteQueue();
}
else
cout << "Stack and queue are empty." << endl;
break;
} //end switch
cin >> num;
} //end while
cout << endl;
cout << "stack: ";
while (!stack.isEmptyStack())
{
cout << stack.top() << " ";
stack.pop();
}
cout << endl;
cout << "queue: ";
while (!queue.isEmptyQueue())
{
cout << queue.front() << " ";
queue.deleteQueue();
}
cout << endl;

SS
Sarvesh Somasundaram
Numerade Educator
01:39

Problem 18

What does the following function do?
void mystery(queueType<int>& q)
{
stackType<int> s;
while (!q.isEmptyQueue())
{
s.push(q.front());
q.deleteQueue();
}
1
7
Exercises | 1217
C7958_C17
while (!s.isEmptyStack())
{
q.addQueue(2 * s.top());
s.pop();
}
}

SS
Sarvesh Somasundaram
Numerade Educator
01:46

Problem 19

Suppose that queue is a queueType object and the size of the array implementing queue is $100 .$ Also, suppose that the value of queueFront is 50 and the value of queueRear is 99
a. What are the values of queueFront and queueRear after adding an element to queue?
b. What are the values of queueFront and queueRear after removing an element from queue?

Adam Dehollander
Adam Dehollander
Numerade Educator
01:46

Problem 20

Suppose that queue is a queueType object and the size of the array implementing queue is $100 .$ Also, suppose that the value of queueFront is 99 and the value of queueRear is 25
a. What are the values of queueFront and queueRear after adding an element to queue?
b. What are the values of queueFront and queueRear after removing an element from queue?

Adam Dehollander
Adam Dehollander
Numerade Educator
01:46

Problem 21

Suppose that queue is a queueType object and the size of the array implementing queue is $100 .$ Also, suppose that the value of queueFront is 25 and the value of queueRear is 75
a. What are the values of queueFront and queueRear after adding an element to queue?
b. What are the values of queueFront and queueRear after removing an element from queue?

Adam Dehollander
Adam Dehollander
Numerade Educator
01:46

Problem 22

Suppose that queue is a queueType object and the size of the array implementing queue is $100 .$ Also, suppose that the value of queueFront is 99 and the value of queueRear is 99
a. What are the values of queueFront and queueRear after adding an element to queue?
b. What are the values of queueFront and queueRear after removing an element from queue?

Adam Dehollander
Adam Dehollander
Numerade Educator
01:46

Problem 23

Suppose that queue is implemented as an array with the special reserved slot, as described in this chapter. Also, suppose that the size of the array implementing queue is $100 .$ If the value of queueFront is $50,$ what is the position of the first queue element?

Adam Dehollander
Adam Dehollander
Numerade Educator
01:46

Problem 24

Suppose that queue is implemented as an array with the special reserved slot, as described in this chapter. Suppose that the size of the array implementing queue is $100 .$ Also, suppose that the value of queueFront is 74 and the value of queueRear is 99
a. What are the values of queueFront and queueRear after adding an element to queue?
b. What are the values of queueFront and queueRear after removing an element from queue? Also, what is the position of the removed queue element?

Adam Dehollander
Adam Dehollander
Numerade Educator
04:49

Problem 25

Write a function template, reverseStack, that takes as a parameter a stack object and uses a queue object to reverse the elements of the stack.

Kris Bright
Kris Bright
Numerade Educator
04:49

Problem 26

Write a function template, reverseQueue, that takes as a parameter a queue object and uses a stack object to reverse the elements of the queue.

Kris Bright
Kris Bright
Numerade Educator
03:16

Problem 27

Add the operation queuecount to the class queueType (the array implementation of queues), which returns the number of elements in the queue. Write the definition of the function template to implement this operation.

SS
Sarvesh Somasundaram
Numerade Educator
02:52

Problem 28

Draw the UML class diagram of the class linkedstackType.

Jordan Gassaway
Jordan Gassaway
Numerade Educator
02:52

Problem 29

Draw the UML class diagram of the class queueADT.

Jordan Gassaway
Jordan Gassaway
Numerade Educator
02:52

Problem 30

Draw the UML class diagram of the class queueType.

Jordan Gassaway
Jordan Gassaway
Numerade Educator
00:47

Problem 31

Draw the UML class diagram of the class linkedQueueType.

Kevin Barayuga
Kevin Barayuga
Numerade Educator