|
ADS
Algorithms and Data Structures
|
\( k \) stacks sharing a vector of linked cells More...
#include <ADS/stack/MultiStack.h>
Public types | |
| using | Item = T |
| the type of the items | |
| using | value_type = T |
| the type of the items | |
| using | const_iterator |
| a constant iterator from a top down along the cells | |
Public member functions | |
| MultiStack (int k) | |
| \( k \) empty stacks | |
| int | stacks (void) const |
| the number \( k \) of stacks | |
| bool | empty (int i) const |
| true if the stack \( i \) has no item | |
| int | size (int i) const |
| the number of items of the stack \( i \) | |
| const T & | top (int i) const |
| the item on top of the stack \( i \) | |
| void | push (int i, const T &v) |
| \( v \) on the stack \( i \); \( v \) may be an item of the stacks, e.g., top( j ) | |
| T | pop (int i) |
| removes the item on top of the stack \( i \) and returns it | |
| std::ranges::subrange< const_iterator > | stack (int i) const |
| the items of the stack \( i \), the top first | |
\( k \) stacks sharing a vector of linked cells
| using ADS::MultiStack< T >::const_iterator |
a constant iterator from a top down along the cells
| using ADS::MultiStack< T >::Item = T |
the type of the items
| using ADS::MultiStack< T >::value_type = T |
the type of the items
|
explicit |
\( k \) empty stacks
|
nodiscard |
true if the stack \( i \) has no item
| T ADS::MultiStack< T >::pop | ( | int | i | ) |
removes the item on top of the stack \( i \) and returns it
In pseudocode (as push()), the cell given back to the free list:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pop}( i ) \cr 2 & \quad c \gets \mathit{top}[ i ];\ v \gets C[ c ].\mathit{value}; \ \mathit{top}[ i ] \gets C[ c ].\mathit{below} \cr 3 & \quad C[ c ].\mathit{below} \gets \mathit{free};\ \mathit{free} \gets c\quad \triangleright\ c \text{ back on the free list} \cr 4 & \quad \textbf{return}\ v \end{array}} \]
| void ADS::MultiStack< T >::push | ( | int | i, |
| const T & | v ) |
\( v \) on the stack \( i \); \( v \) may be an item of the stacks, e.g., top( j )
In pseudocode (exercise 4.7, with a free list as in Knuth, section 2.2.3), \( C \) the cells, \( \mathit{top}[ i ] \) the top cell of the stack \( i \), -1 for none:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( i , v ) \cr 2 & \quad \textbf{if}\ \mathit{free} = -1\ \textbf{then}\ c \gets \text{a new cell at the end of } C\quad \triangleright \ \text{enlarging } C \text{ if full} \cr 3 & \quad \textbf{else}\ c \gets \mathit{free};\ \mathit{free} \gets C[ c ].\mathit{below} \cr 4 & \quad C[ c ] \gets ( v , \mathit{top}[ i ] );\ \mathit{top}[ i ] \gets c \end{array}} \]
|
nodiscard |
the number of items of the stack \( i \)
|
inlinenodiscard |
the items of the stack \( i \), the top first
|
nodiscard |
the number \( k \) of stacks
|
nodiscard |
the item on top of the stack \( i \)