ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
StackAlgorithms.h File Reference

Algorithms that use a STACK [see ADT.h] only through its operations, beyond the book: More...

#include "ADS/ADT.h"
#include "ADS/Contract.h"
#include "ADS/queue/LinkedQueue.h"
#include "ADS/sequence/List.h"
#include "ADS/stack/ArrayStack.h"
#include "ADS/stack/SequenceStack.h"
#include <algorithm>
#include <span>
#include <string>
#include <string_view>
#include <vector>
#include "ADS/stack/impl/StackAlgorithms.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

bool ADS::isPalindrome (const std::string &s)
 whether \( s \) reads the same from both ends
template<Stack S>
void ADS::sortStack (S &s)
 sorts \( s \) with the least item on top
template<Stack S>
void ADS::removeGreater (S &s, const typename S::Item &k)
 removes the items larger than \( k \), the others in their order
template<Stack S, class F>
void ADS::forEachBottomUp (S &s, F &&f)
 f( x ) on each item \( x \), from the bottom to the top
bool ADS::isDyckWord (std::span< const int > w)
 whether the sequence \( w \) of \( +1 \) and \( -1 \) is a Dyck word, i.e., has no negative prefix sum
bool ADS::isMatched (std::string_view s)
 whether \( s \), a string of the brackets (), [] and {}, is matched, in \( O( n ) \)
template<Stack S, Queue Q = LinkedQueue< typename S::Item >>
void ADS::reverseWithQueue (S &s)
 the order of the items of \( s \) reversed by a QUEUE, the only operations of \( s \) being push() and pop()

Detailed description

Algorithms that use a STACK [see ADT.h] only through its operations, beyond the book:

  • isPalindrome(), the characters pushed and then popped against the string from its start, \( O( n ) \);
  • sortStack(), which sorts a stack with its own operations and the recursion: a pass pops two items, pushes back the larger, makes a pass on the rest recursively and pushes the smaller on top, so that the largest item sinks to the bottom and the smaller ones rise by one place; \( n - 1 \) passes sort the stack, the least on top, in \( \Theta( n^2 ) \);
  • removeGreater(), which removes the items larger than \( k \) keeping the order of the others, with an auxiliary stack;
  • forEachBottomUp(), which visits the items from the bottom to the top and leaves the stack as it was: the top is popped, the rest visited recursively, then the top visited and pushed back, in \( O( n ) \) time and \( O( n ) \) space for the recursion;
  • isDyckWord(), whether a sequence of \( +1 \) and \( -1 \) has no negative prefix sum (exercise 1.2 of Morin's book): it does exactly when the pushes for \( +1 \) and the pops for \( -1 \), run on an empty stack, never pop an empty stack, the prefix sum being the size of the stack; a stack of items all alike is a counter, which the function keeps;
  • isMatched(), whether a string of the brackets (), [] and {} is matched (exercise 1.3 of Morin's book): an open bracket is pushed, a closed one must find its open twin on top and pops it, and the stack must be empty at the end, \( O( n ) \); it is the Dyck word of the sizes of the stack with the kinds of the brackets on its items;
  • reverseWithQueue(), the order of a stack reversed by a QUEUE alone (exercise 1.4 of Morin's book): the items popped into the queue come out of it in the order they went in, the old top first, and pushed back in that order they leave the old top at the bottom, \( O( n ) \).

The contracts [see Contract.h]: sortStack() ensures a sorted stack with the same number of items, removeGreater() a stack with no item larger than \( k \), forEachBottomUp() the stack as it was, and reverseWithQueue() the items in the reverse order; all are audits, checked by popping the stack and pushing it back. The algorithms are defined in StackAlgorithms.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 4.3.1, https://algoritmicamente.it/materiali/libro/capitoli/cap04.pdf
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, exercises 1.2, 1.3 and 1.4
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa