ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::SinglyList< T > Class Template Reference

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 ) \)

Detailed description

template<class T>
class ADS::SinglyList< T >

a SEQUENCE realized as a singly linked list with no sentinel

Member typedef documentation

◆ const_iterator

template<class T>
using ADS::SinglyList< T >::const_iterator = Iterator< true >

the iterator on the constant items

◆ const_reference

template<class T>
using ADS::SinglyList< T >::const_reference = const T &

a constant reference to an item

◆ difference_type

template<class T>
using ADS::SinglyList< T >::difference_type = std::ptrdiff_t

the type of the distances

◆ Item

template<class T>
using ADS::SinglyList< T >::Item = T

the type of the items

◆ iterator

template<class T>
using ADS::SinglyList< T >::iterator = Iterator< false >

the iterator on the items

◆ Pos

template<class T>
using ADS::SinglyList< T >::Pos = Cell *

the cell before the item, nil for the first one

◆ reference

template<class T>
using ADS::SinglyList< T >::reference = T &

a reference to an item

◆ size_type

template<class T>
using ADS::SinglyList< T >::size_type = std::size_t

the type of the sizes

◆ value_type

template<class T>
using ADS::SinglyList< T >::value_type = T

the type of the items

Constructor and destructor documentation

◆ SinglyList() [1/5]

template<class T>
ADS::SinglyList< T >::SinglyList ( void )
default

the empty list

◆ SinglyList() [2/5]

template<class T>
ADS::SinglyList< T >::SinglyList ( std::initializer_list< T > items)

the list of the given items, in their order

◆ SinglyList() [3/5]

template<class T>
ADS::SinglyList< T >::SinglyList ( const SinglyList< T > & other)

a copy of other, cell by cell

◆ SinglyList() [4/5]

template<class T>
ADS::SinglyList< T >::SinglyList ( SinglyList< T > && other)
noexcept

the cells of other, which is left empty

◆ ~SinglyList()

template<class T>
ADS::SinglyList< T >::~SinglyList ( )

frees the cells

◆ SinglyList() [5/5]

template<class T>
template<std::input_iterator I, std::sentinel_for< I > S>
ADS::SinglyList< T >::SinglyList ( I first,
S last )

the list of the items of [ first , last ), in their order

Member function documentation

◆ add()

template<class T>
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}} \]

Precondition
\( i \le n \)
Postcondition
one item more, \( x \) at the rank \( i \)

◆ back()

template<class T>
const T & ADS::SinglyList< T >::back ( void ) const
nodiscard

the last item, from the last cell, \( O( 1 ) \)

Precondition
the list is not empty

◆ begin() [1/2]

template<class T>
iterator ADS::SinglyList< T >::begin ( void )
inlinenodiscard

the iterator on the first item

◆ begin() [2/2]

template<class T>
const_iterator ADS::SinglyList< T >::begin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cbegin()

template<class T>
const_iterator ADS::SinglyList< T >::cbegin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cend()

template<class T>
const_iterator ADS::SinglyList< T >::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ checkSize()

template<class T>
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 ) \)

◆ empty()

template<class T>
bool ADS::SinglyList< T >::empty ( void ) const
nodiscard

true if there is no item

◆ end() [1/2]

template<class T>
iterator ADS::SinglyList< T >::end ( void )
inlinenodiscard

the iterator past the last item

◆ end() [2/2]

template<class T>
const_iterator ADS::SinglyList< T >::end ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ erase()

template<class T>
T ADS::SinglyList< T >::erase ( std::size_t i)

removes the item of rank \( i \) and returns it, \( O( 1 + i ) \) (exercise 3.3)

Precondition
\( i < n \)
Postcondition
one item less

◆ finished()

template<class T>
bool ADS::SinglyList< T >::finished ( Pos p) const
nodiscard

true on \( \mathit{pos}_{-1} \) and on \( \mathit{pos}_n \), the address of the last cell (nil if the list is empty)

◆ front()

template<class T>
const T & ADS::SinglyList< T >::front ( void ) const
nodiscard

the first item

Precondition
the list is not empty

◆ get()

template<class T>
const T & ADS::SinglyList< T >::get ( std::size_t i) const
nodiscard

the item of rank \( i \), by a walk of \( i \) cells (exercise 3.3 of Morin's book)

Precondition
\( i < n \)

◆ head()

template<class T>
Pos ADS::SinglyList< T >::head ( void ) const
nodiscard

\( \mathit{pos}_0 \), i.e., nil

◆ insert()

template<class T>
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}} \]

Precondition
\( p \) is not \( \mathit{pos}_{-1} \)
Postcondition
read( p ) is \( v \)

◆ next()

template<class T>
Pos ADS::SinglyList< T >::next ( Pos p) const
nodiscard

the position that follows \( p \)

Precondition
\( p \) is not \( \mathit{pos}_n \)

◆ operator=()

template<class T>
SinglyList & ADS::SinglyList< T >::operator= ( SinglyList< T > other)
noexcept

the items of other, by copy and swap

◆ operator==()

template<class T>
bool ADS::SinglyList< T >::operator== ( const SinglyList< T > & o) const
nodiscard

the same items in the same order

◆ pop_front()

template<class T>
T ADS::SinglyList< T >::pop_front ( void )

removes the first item and returns it (Morin's pop() and remove()), \( O( 1 ) \)

Precondition
the list is not empty

◆ prev()

template<class T>
Pos ADS::SinglyList< T >::prev ( Pos p) const
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}} \]

◆ push_back()

template<class T>
void ADS::SinglyList< T >::push_back ( const T & v)

\( v \) at the back, after the last cell (Morin's add( x )), \( O( 1 ) \)

◆ push_front()

template<class T>
void ADS::SinglyList< T >::push_front ( const T & v)

\( v \) at the front, for std::front_inserter (Morin's push( x )), \( O( 1 ) \)

◆ read()

template<class T>
const T & ADS::SinglyList< T >::read ( Pos p) const
nodiscard

the item in the position \( p \)

Precondition
\( p \) holds an item

◆ remove()

template<class T>
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}} \]

Precondition
\( p \) holds an item

◆ reverse()

template<class T>
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}} \]

Postcondition
the items in the reverse order (an audit)

◆ secondLast()

template<class T>
const T & ADS::SinglyList< T >::secondLast ( void ) const
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}} \]

Precondition
two items at least

◆ set()

template<class T>
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)

Precondition
\( i < n \)

◆ size()

template<class T>
std::size_t ADS::SinglyList< T >::size ( void ) const
inlinenodiscard

the number \( n \) of items, kept

◆ swap()

template<class T>
void ADS::SinglyList< T >::swap ( SinglyList< T > & other)
noexcept

exchanges the cells with other

◆ tail()

template<class T>
Pos ADS::SinglyList< T >::tail ( void ) const
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}} \]

◆ write()

template<class T>
void ADS::SinglyList< T >::write ( Pos p,
const T & v )

writes \( v \) in the position \( p \)

Precondition
\( p \) holds an item
Postcondition
read( p ) is \( v \)

The documentation for this class was generated from the following file: