|
ADS
Algorithms and Data Structures
|
a SEQUENCE realized as a singly linked list with no sentinel More...
#include <ADS/sequence/SinglyList.h>
Classes | |
| struct | Cell |
| a cell of the list More... | |
| class | Iterator |
| the iterator, on items const if Const More... | |
Public types | |
| using | Item = T |
| the type of the items | |
| using | Pos = Cell * |
| the cell before the item, nil for the first one | |
| using | value_type = T |
| the type of the items | |
| using | size_type = std::size_t |
| the type of the sizes | |
| using | difference_type = std::ptrdiff_t |
| the type of the distances | |
| using | reference = T & |
| a reference to an item | |
| using | const_reference = const T & |
| a constant reference to an item | |
| using | iterator = Iterator< false > |
| the iterator on the items | |
| using | const_iterator = Iterator< true > |
| the iterator on the constant items | |
Public member functions | |
| SinglyList (void)=default | |
| the empty list | |
| SinglyList (std::initializer_list< T > items) | |
| the list of the given items, in their order | |
| SinglyList (const SinglyList &other) | |
| a copy of other, cell by cell | |
| SinglyList (SinglyList &&other) noexcept | |
| the cells of other, which is left empty | |
| SinglyList & | operator= (SinglyList other) noexcept |
| the items of other, by copy and swap | |
| ~SinglyList () | |
| frees the cells | |
| void | swap (SinglyList &other) noexcept |
| exchanges the cells with other | |
| bool | empty (void) const |
| true if there is no item | |
| bool | finished (Pos p) const |
| true on \( \mathit{pos}_{-1} \) and on \( \mathit{pos}_n \), the address of the last cell (nil if the list is empty) | |
| Pos | head (void) const |
| \( \mathit{pos}_0 \), i.e., nil | |
| Pos | tail (void) const |
| the position of the last item, \( O( n ) \) | |
| Pos | next (Pos p) const |
| the position that follows \( p \) | |
| Pos | prev (Pos p) const |
| the position that precedes \( p \), \( O( n ) \) | |
| Pos | insert (Pos p, const T &v) |
| puts \( v \) in the position \( p \), i.e., before the item in \( p \); the new item takes the position \( p \) | |
| Pos | remove (Pos p) |
| removes the item in the position \( p \), whose successor takes the position \( p \) | |
| const T & | read (Pos p) const |
| the item in the position \( p \) | |
| void | write (Pos p, const T &v) |
| writes \( v \) in the position \( p \) | |
| bool | operator== (const SinglyList &o) const |
| the same items in the same order | |
| iterator | begin (void) |
| the iterator on the first item | |
| iterator | end (void) |
| the iterator past the last item | |
| const_iterator | begin (void) const |
| the constant iterator on the first item | |
| const_iterator | end (void) const |
| the constant iterator past the last item | |
| const_iterator | cbegin (void) const |
| the constant iterator on the first item | |
| const_iterator | cend (void) const |
| the constant iterator past the last item | |
| template<std::input_iterator I, std::sentinel_for< I > S> | |
| SinglyList (I first, S last) | |
| the list of the items of [ first , last ), in their order | |
| void | push_front (const T &v) |
| \( v \) at the front, for std::front_inserter (Morin's push( x )), \( O( 1 ) \) | |
| std::size_t | size (void) const |
| the number \( n \) of items, kept | |
| void | push_back (const T &v) |
| \( v \) at the back, after the last cell (Morin's add( x )), \( O( 1 ) \) | |
| T | pop_front (void) |
| removes the first item and returns it (Morin's pop() and remove()), \( O( 1 ) \) | |
| const T & | front (void) const |
| the first item | |
| const T & | back (void) const |
| the last item, from the last cell, \( O( 1 ) \) | |
| const T & | get (std::size_t i) const |
| the item of rank \( i \), by a walk of \( i \) cells (exercise 3.3 of Morin's book) | |
| T | set (std::size_t i, const T &x) |
| \( x \) written at the rank \( i \), \( O( 1 + i ) \); the old item returned (exercise 3.3) | |
| void | add (std::size_t i, const T &x) |
| \( x \) put at the rank \( i \), after the cell of rank \( i - 1 \) reached by a walk, \( O( 1 + i ) \) (exercise 3.3) | |
| T | erase (std::size_t i) |
| removes the item of rank \( i \) and returns it, \( O( 1 + i ) \) (exercise 3.3) | |
| const T & | secondLast (void) const |
| the item before the last one, by a walk that reads neither the size nor the last cell (exercise 3.2 of Morin's book), \( O( n ) \) | |
| void | reverse (void) |
| the order of the items reversed by relinking the cells, in \( O( n ) \) time and \( O( 1 ) \) space, with no cell made (exercise 3.4 of Morin's book) | |
| void | checkSize (void) const |
| counts the cells and throws std::logic_error if they are not the size kept (exercise 3.5 of Morin's book), \( O( n ) \) | |
a SEQUENCE realized as a singly linked list with no sentinel
| using ADS::SinglyList< T >::const_iterator = Iterator< true > |
the iterator on the constant items
| using ADS::SinglyList< T >::const_reference = const T & |
a constant reference to an item
| using ADS::SinglyList< T >::difference_type = std::ptrdiff_t |
the type of the distances
| using ADS::SinglyList< T >::Item = T |
the type of the items
| using ADS::SinglyList< T >::iterator = Iterator< false > |
the iterator on the items
| using ADS::SinglyList< T >::Pos = Cell * |
the cell before the item, nil for the first one
| using ADS::SinglyList< T >::reference = T & |
a reference to an item
| using ADS::SinglyList< T >::size_type = std::size_t |
the type of the sizes
| using ADS::SinglyList< T >::value_type = T |
the type of the items
|
default |
the empty list
| ADS::SinglyList< T >::SinglyList | ( | std::initializer_list< T > | items | ) |
the list of the given items, in their order
| ADS::SinglyList< T >::SinglyList | ( | const SinglyList< T > & | other | ) |
a copy of other, cell by cell
|
noexcept |
the cells of other, which is left empty
| ADS::SinglyList< T >::~SinglyList | ( | ) |
frees the cells
| ADS::SinglyList< T >::SinglyList | ( | I | first, |
| S | last ) |
the list of the items of [ first , last ), in their order
| void ADS::SinglyList< T >::add | ( | std::size_t | i, |
| const T & | x ) |
\( x \) put at the rank \( i \), after the cell of rank \( i - 1 \) reached by a walk, \( O( 1 + i ) \) (exercise 3.3)
In pseudocode (Morin, exercise 3.3), \( \mathit{first} \) the first cell:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{add}( i , x ) \cr 2 & \quad \textbf{if}\ i = 0\ \textbf{then}\ \mathrm{pushFront}( x ); \ \textbf{return} \cr 3 & \quad u \gets \mathit{first};\ \textbf{repeat}\ i - 1 \ \text{ times: } u \gets u.\mathit{next} \cr 4 & \quad u.\mathit{next} \gets \text{a new cell of } x \text{ before } u.\mathit{next};\ n \gets n + 1 \end{array}} \]
|
nodiscard |
the last item, from the last cell, \( O( 1 ) \)
|
inlinenodiscard |
the iterator on the first item
|
inlinenodiscard |
the constant iterator on the first item
|
inlinenodiscard |
the constant iterator on the first item
|
inlinenodiscard |
the constant iterator past the last item
| void ADS::SinglyList< T >::checkSize | ( | void | ) | const |
counts the cells and throws std::logic_error if they are not the size kept (exercise 3.5 of Morin's book), \( O( n ) \)
|
nodiscard |
true if there is no item
|
inlinenodiscard |
the iterator past the last item
|
inlinenodiscard |
the constant iterator past the last item
| T ADS::SinglyList< T >::erase | ( | std::size_t | i | ) |
removes the item of rank \( i \) and returns it, \( O( 1 + i ) \) (exercise 3.3)
|
nodiscard |
true on \( \mathit{pos}_{-1} \) and on \( \mathit{pos}_n \), the address of the last cell (nil if the list is empty)
|
nodiscard |
the first item
|
nodiscard |
the item of rank \( i \), by a walk of \( i \) cells (exercise 3.3 of Morin's book)
|
nodiscard |
\( \mathit{pos}_0 \), i.e., nil
| Pos ADS::SinglyList< T >::insert | ( | Pos | p, |
| const T & | v ) |
puts \( v \) in the position \( p \), i.e., before the item in \( p \); the new item takes the position \( p \)
In pseudocode (exercise 4.6), the new cell after the cell \( p \), or first if \( p \) is nil:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( p , v ) \cr 2 & \quad \ell \gets \mathit{first} \text{ if } p = \textbf{nil} \text{, else } p.\mathit{next}\quad \triangleright \ \text{the link to change} \cr 3 & \quad \ell \gets \text{a new cell of value } v \text{ whose } \mathit{next} \text{ is the old } \ell \cr 4 & \quad \textbf{return}\ p \end{array}} \]
|
nodiscard |
the position that follows \( p \)
|
noexcept |
the items of other, by copy and swap
|
nodiscard |
the same items in the same order
| T ADS::SinglyList< T >::pop_front | ( | void | ) |
removes the first item and returns it (Morin's pop() and remove()), \( O( 1 ) \)
|
nodiscard |
the position that precedes \( p \), \( O( n ) \)
In pseudocode (exercise 4.6), a scan for the cell whose successor is \( p \), since no link goes back:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{prev}( p ) \cr 2 & \quad \textbf{if}\ p = \textbf{nil}\ \textbf{then return} \ \mathit{pos}_{-1};\ \textbf{if}\ p = \mathit{first} \ \textbf{then return nil} \cr 3 & \quad q \gets \text{the cell of the list whose } \mathit{next} \text{ is } p \text{, by a scan from } \mathit{first} \cr 4 & \quad \textbf{return}\ q \end{array}} \]
| void ADS::SinglyList< T >::push_back | ( | const T & | v | ) |
\( v \) at the back, after the last cell (Morin's add( x )), \( O( 1 ) \)
| void ADS::SinglyList< T >::push_front | ( | const T & | v | ) |
\( v \) at the front, for std::front_inserter (Morin's push( x )), \( O( 1 ) \)
|
nodiscard |
the item in the position \( p \)
| Pos ADS::SinglyList< T >::remove | ( | Pos | p | ) |
removes the item in the position \( p \), whose successor takes the position \( p \)
In pseudocode (exercise 4.6), the cell after \( p \) unlinked:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{remove}( p ) \cr 2 & \quad \ell \gets \mathit{first} \text{ if } p = \textbf{nil} \text{, else } p.\mathit{next};\ t \gets \text{the cell } \ell \text{ points to} \cr 3 & \quad \ell \gets t.\mathit{next};\ \text{free the cell } t \cr 4 & \quad \textbf{return}\ p\quad \triangleright \ \text{now before the next item} \end{array}} \]
| void ADS::SinglyList< T >::reverse | ( | void | ) |
the order of the items reversed by relinking the cells, in \( O( n ) \) time and \( O( 1 ) \) space, with no cell made (exercise 3.4 of Morin's book)
In pseudocode (Morin, exercise 3.4):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{reverse}() \cr 2 & \quad p \gets \textbf{nil};\ u \gets \mathit{first}; \ \mathit{last} \gets u \cr 3 & \quad \textbf{while}\ u \ne \textbf{nil}\ \textbf{do} \ w \gets u.\mathit{next};\ u.\mathit{next} \gets p;\ p \gets u; \ u \gets w \cr 4 & \quad \mathit{first} \gets p \end{array}} \]
|
nodiscard |
the item before the last one, by a walk that reads neither the size nor the last cell (exercise 3.2 of Morin's book), \( O( n ) \)
In pseudocode (Morin, exercise 3.2):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{secondLast}() \cr 2 & \quad u \gets \mathit{first} \cr 3 & \quad \textbf{while}\ u.\mathit{next}.\mathit{next} \ne \textbf{nil}\ \textbf{do}\ u \gets u.\mathit{next} \cr 4 & \quad \textbf{return}\ u.\mathit{value} \end{array}} \]
| T ADS::SinglyList< T >::set | ( | std::size_t | i, |
| const T & | x ) |
\( x \) written at the rank \( i \), \( O( 1 + i ) \); the old item returned (exercise 3.3)
|
inlinenodiscard |
the number \( n \) of items, kept
|
noexcept |
exchanges the cells with other
|
nodiscard |
the position of the last item, \( O( n ) \)
In pseudocode (exercise 4.6), the cell before the last one found by a scan:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{tail}() \cr 2 & \quad \textbf{if}\ \mathit{first} = \textbf{nil} \ \textbf{then return}\ \mathit{pos}_{-1}\quad \triangleright \ \text{no item} \cr 3 & \quad p \gets \textbf{nil}\quad \triangleright \ \text{the position of the first item} \cr 4 & \quad \textbf{while not}\ \mathrm{finished}( \mathrm{next}( p ) ) \ \textbf{do}\ p \gets \mathrm{next}( p ) \cr 5 & \quad \textbf{return}\ p \end{array}} \]
| void ADS::SinglyList< T >::write | ( | Pos | p, |
| const T & | v ) |
writes \( v \) in the position \( p \)