ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
QueueAlgorithms.h

Algorithms that use a QUEUE only through its operations, beyond the book. More...

Files

file  ADS/queue/QueueAlgorithms.h
 Algorithms that use a QUEUE [see ADT.h] only through its operations, beyond the book:

Functions

template<Queue Q>
void ADS::extractPositive (Q &q, Q &out)
 moves the positive items of \( q \), in their order, to the back of out, leaving \( q \) empty
template<Queue Q>
void ADS::copyPositive (Q &q, Q &out)
 copies the positive items of \( q \), in their order, to the back of out, leaving \( q \) as it was

Detailed description

Algorithms that use a QUEUE only through its operations, beyond the book.

The definitions, the invariants and the costs of the area are in Theory of queues.

Function documentation

◆ copyPositive()

template<Queue Q>
void ADS::copyPositive ( Q & q,
Q & out )

copies the positive items of \( q \), in their order, to the back of out, leaving \( q \) as it was

In pseudocode (section 4.3.1), an auxiliary queue \( A \) keeping the order of \( q \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{copyPositive}( q , \mathit{out} ) \cr 2 & \quad A \gets \text{an empty queue}\quad \triangleright \ \text{the items of } q \text{, in order} \cr 3 & \quad \textbf{while not}\ \mathrm{empty}( q )\ \textbf{do} \cr 4 & \qquad x \gets \mathrm{dequeue}( q ) \cr 5 & \qquad \textbf{if}\ 0 < x\ \textbf{then}\ \mathrm{enqueue}( \mathit{out} , x ) \cr 6 & \qquad \mathrm{enqueue}( A , x ) \cr 7 & \quad \textbf{while not}\ \mathrm{empty}( A )\ \textbf{do} \ \mathrm{enqueue}( q , \mathrm{dequeue}( A ) ) \end{array}} \]

Precondition
\( q \) and out are distinct
Postcondition
\( q \) is as it was (an audit)

◆ extractPositive()

template<Queue Q>
void ADS::extractPositive ( Q & q,
Q & out )

moves the positive items of \( q \), in their order, to the back of out, leaving \( q \) empty

In pseudocode (section 4.3.1):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{extractPositive}( q , \mathit{out} ) \cr 2 & \quad \textbf{while not}\ \mathrm{empty}( q )\ \textbf{do}\quad \triangleright\ q \text{ ends empty} \cr 3 & \qquad x \gets \mathrm{dequeue}( q );\ \textbf{if}\ 0 < x \ \textbf{then}\ \mathrm{enqueue}( \mathit{out} , x ) \end{array}} \]

Precondition
\( q \) and out are distinct
Postcondition
\( q \) is empty