• Home
  • Textbooks
  • Data Structures and Algorithms in C++
  • STACKS AND QUEUES

Data Structures and Algorithms in C++

Adam Drozdek

Chapter 4

STACKS AND QUEUES - all with Video Answers

Educators


Chapter Questions

04:49

Problem 1

Reverse the order of elements on stack $\mathrm{S}$
a. using two additional stacks
b. using one additional queue
c. using one additional stack and some additional variables

Kris Bright
Kris Bright
Numerade Educator

Problem 2

Put the elements on the stack $S$ in ascending order using one additional stack and some additional variables.

Check back soon!
01:54

Problem 3

Transfer elements from stack $S_1$ to stack $S_2$ so that the elements from $S_2$ are in the same order as on $\mathrm{S}_1$
a. using one additional stack
b. using no additional stack but only some additional variables

SS
Sarvesh Somasundaram
Numerade Educator

Problem 4

Suggest an implementation of a stack to hold elements of two different types, such as structures and float numbers.

Check back soon!
03:16

Problem 5

Using additional variables, order all elements on a queue using also
a. two additional queues
b. one additional queue

SS
Sarvesh Somasundaram
Numerade Educator
00:57

Problem 6

In this chapter, two different implementations were developed for a stack: class Stack and class LLStack. The names of member functions in both classes suggest that the same data structure is meant; however, a tighter connection between these two classes can be established. Define an abstract base class for a stack and derive from it both class Stack and class LLStack.

Aditya Sood
Aditya Sood
Numerade Educator
03:49

Problem 7

In the class Stack, no test is made in push ( ) for stack overflow. The test should be made by the user to avoid abnormal program termination. Similarly, no test for stack underflow is made in pop ( ) and in topEl ( ). The tests can be incorporated in all these member functions in a variety of ways. Here are some examples.
a. void push(const $\mathrm{T} \& \mathrm{e}$, , bool& pushed) $\{\ldots\}$
In this definition, pushed would be set by push ( ) to false if stack is full and to true if el can be pushed onto the stack. The variable pushed is tested by the user after push ( ) is finished.
b. bool push(const $\mathrm{T} \&$ el) $\{\ldots\}$
In this version, the return value plays the same role as pushed in the previous version. push ( ) returns true if el was pushed successfully, false otherwise.
c. void push(const $T \&$ el) {
if (isfull()) $\operatorname{exit}(0)$;
else...
Interrupt the program when an attempt is made to push an element onto a full stack, possibly printing an error message, and push the element on the stack otherwise.
What are the disadvantages of these definitions?

SS
Sarvesh Somasundaram
Numerade Educator
03:16

Problem 8

Define a stack in terms of a queue; that is, create a class
template <class $\mathrm{T}>$
class stackQ {
Queue<T> pool;
void push(const $\mathrm{T} \& \mathrm{el}$.
pool.enqueue (el);
. . . . . . . . .
template <class $\mathrm{T}>$
class stackQ {
Queue<T> pool;
void push(const $\mathrm{T} \&$ el) {
pool. enqueue (el);

SS
Sarvesh Somasundaram
Numerade Educator
04:49

Problem 9

Define a queue in terms of a stack.

Kris Bright
Kris Bright
Numerade Educator

Problem 10

A generic queue class by defined in terms of a vector:
template<class $T$, int capacity $=30>$
class QueueV {
. . . . . . .
private:
vector<T> pool;
}
template<class $T$, int capacity $=30>$
class QueueV {
. . . . . . . .
private:
vector $\langle\mathrm{T}\rangle$ pool;
}
Is this a viable solution?

Check back soon!
01:56

Problem 11

Modify the program from the case study to print out the path without dead ends and, possibly, with detours. For example, for an input maze
$$
\begin{aligned}
& 1111111 \\
& 1 \mathrm{e} 00001 \\
& 1110111 \\
& 1000001 \\
& 100 \mathrm{~m} 001 \\
& 1111111
\end{aligned}
$$
the program from the case study outputs the processed maze
$$
\begin{aligned}
& 1111111 \\
& 1 \mathrm{e} \ldots .1 \\
& 111.111 \\
& 1 \ldots .1 \\
& 1 \ldots \mathrm{m} .1 \\
& 1111111 \\
& \text { Success }
\end{aligned}
$$
The modified program should, in addition, generate the path from the exit to the mouse:
$$
\left[\begin{array}{ll}
1 & 1
\end{array}\right]\left[\begin{array}{ll}
1 & 2
\end{array}\right]\left[\begin{array}{ll}
1 & 3
\end{array}\right]\left[\begin{array}{ll}
2 & 3
\end{array}\right]\left[\begin{array}{ll}
3 & 3
\end{array}\right]\left[\begin{array}{ll}
3 & 4
\end{array}\right]\left[\begin{array}{ll}
3 & 5
\end{array}\right]\left[\begin{array}{ll}
4 & 5
\end{array}\right]\left[\begin{array}{ll}
4 & 4
\end{array}\right]\left[\begin{array}{ll}
4 & 3
\end{array}\right]
$$
but retains a detour, $\left[\begin{array}{ll}3 & 4\end{array}\right]\left[\begin{array}{ll}3 & 5\end{array}\right]\left[\begin{array}{ll}4 & 5\end{array}\right]\left[\begin{array}{ll}4 & 4\end{array}\right]$.

Narayan Hari
Narayan Hari
Numerade Educator

Problem 12

12. Modify the program from the previous exercise so that it prints the maze with the path without dead ends; the path is indicated by dashes and vertical bars to indicate the changes of direction of the path; for the input maze from the previous exercise, the modified program should output
$$
\begin{aligned}
& 1111111 \\
& 1 \mathrm{e}--\ldots 1 \\
& 111 \mid 111 \\
& 1 \ldots \mid--1 \\
& 1 \ldots \mathrm{m}-\mid 1 \\
& 1111111
\end{aligned}
$$

Check back soon!