AlgoPlus v0.1.0
Loading...
Searching...
No Matches
frequency_list< T >::Iterator Class Reference

Public Member Functions

 Iterator (const std::shared_ptr< node > &ptr) noexcept
 Iterator class for a linked list.
 
Iteratoroperator= (const std::shared_ptr< node > &ptr)
 Assignment operator for the Iterator class.
 
Iteratoroperator++ ()
 Pre-increment operator for the Iterator class.
 
Iterator operator++ (int)
 Post-increment operator for the Iterator class.
 
Iteratoroperator-- ()
 Pre-increment – operator for the Iterator class. This operator overloads the pre-increment operator (–). It increments the iterator to the next node and returns a reference to the updated iterator.
 
Iterator operator-- (int)
 Post-increment –operator for the Iterator class. This operator overloads the post-increment operator (–). It increments the iterator to the next node and returns a copy of the iterator before the increment.
 
bool operator== (const Iterator &it)
 Equality operator for the Iterator class.
 
bool operator!= (const Iterator &it)
 Inequality operator for the Iterator class.
 
operator* ()
 Dereference operator for the Iterator class.
 
 frequency_list (std::vector< T > data={}) noexcept
 Constructs a frequency_list object with an optional initializer list of elements.
 
 frequency_list (const frequency_list< T > &list)
 Copy constructor for the frequency_list class.
 
void push_back (T data)
 Adds an element to the back of the frequency list.
 
void push_front (T data)
 Adds an element to the front of the frequency list.
 
bool search (T key)
 Searches for a given key in the frequency list.
 
int64_t get_frequency (T key)
 Gets the frequency of a key in the frequency list.
 
void erase (T key)
 Removes the first occurrence of a given key in the frequency list.
 
void reset_frequency ()
 Resets the frequency of all nodes in the frequency list to 1.
 
std::vector< std::pair< T, int64_t > > elements ()
 Returns all the elements of the list.
 
bool empty ()
 Checks if the frequency list is empty.
 
Iterator begin ()
 Get an iterator pointing to the beginning of the frequency list.
 
Iterator end ()
 Get an iterator pointing to the end of the frequency list.
 
void visualize ()
 visualize function for frequency_list Class returns a .dot file that can be previewd with graphviz plugin in vscode
 

Friends

std::ostream & operator<< (std::ostream &os, const frequency_list< T > &flist)
 Overloaded output stream insertion operator for the frequency_list class.
 

Constructor & Destructor Documentation

◆ Iterator()

template<typename T>
frequency_list< T >::Iterator::Iterator ( const std::shared_ptr< node > & ptr)
inlineexplicitnoexcept

Iterator class for a linked list.

The Iterator class provides an iterator for traversing a linked list.

Member Function Documentation

◆ begin()

Iterator frequency_list< T >::begin ( )
inline

Get an iterator pointing to the beginning of the frequency list.

The Iterator provides a way to traverse the frequency list.

Returns
An Iterator object pointing to the beginning of the frequency list.

◆ elements()

std::vector< std::pair< T, int64_t > > frequency_list< T >::elements ( )
inline

Returns all the elements of the list.

Returns
std::vector<T> the elements of the list

◆ empty()

bool frequency_list< T >::empty ( )
inline

Checks if the frequency list is empty.

Returns
Returns true if the frequency list is empty, false otherwise.

◆ end()

Iterator frequency_list< T >::end ( )
inline

Get an iterator pointing to the end of the frequency list.

The end iterator is used to indicate the position after the last element of the frequency list, and can be used to determine when the end of the frequency list has been reached while iterating.

Returns
An Iterator object pointing to the end of the frequency list.

◆ erase()

void frequency_list< T >::erase ( T key)
inline

Removes the first occurrence of a given key in the frequency list.

If the key is found, it removes the corresponding node and updates the pointers. If the head node is the one to be removed, the head pointer is updated accordingly. If the node has a previous and/or next node, their pointers are updated to connect them together.

Parameters
keyThe key to be removed from the frequency list.

◆ frequency_list() [1/2]

frequency_list< T >::frequency_list ( const frequency_list< T > & list)
inline

Copy constructor for the frequency_list class.

This constructor creates a new frequency_list object by copying the elements from another frequency_list object. The elements are copied in the same order as they appear in the original frequency_list object.

Parameters
listThe frequency_list object to copy from.

◆ frequency_list() [2/2]

frequency_list< T >::frequency_list ( std::vector< T > data = {})
inlineexplicitnoexcept

Constructs a frequency_list object with an optional initializer list of elements.

The frequency_list object is constructed with an optional initializer list of elements. If the initializer list is not empty, the elements are added to the frequency_list in the same order as they appear in the vector.

Template Parameters
TThe type of elements stored in the frequency_list.
Parameters
dataAn optional initializer list of elements.

◆ get_frequency()

int64_t frequency_list< T >::get_frequency ( T key)
inline

Gets the frequency of a key in the frequency list.

Parameters
keyThe key to get the frequency of.
Returns
The frequency of the key, or -1 if the key is not found.

◆ operator!=()

template<typename T>
bool frequency_list< T >::Iterator::operator!= ( const Iterator & it)
inline

Inequality operator for the Iterator class.

This operator checks if two iterators are not equal by comparing their current nodes.

Parameters
itThe other Iterator object to compare with.
Returns
true if the iterators are not equal, false otherwise.

◆ operator*()

template<typename T>
T frequency_list< T >::Iterator::operator* ( )
inline

Dereference operator for the Iterator class.

Returns
The data pointed to by the current node.

◆ operator++() [1/2]

template<typename T>
Iterator & frequency_list< T >::Iterator::operator++ ( )
inline

Pre-increment operator for the Iterator class.

This operator overloads the pre-increment operator (++). It increments the iterator to the next node and returns a reference to the updated iterator.

Returns
A reference to the updated iterator.

◆ operator++() [2/2]

template<typename T>
Iterator frequency_list< T >::Iterator::operator++ ( int )
inline

Post-increment operator for the Iterator class.

This operator overloads the post-increment operator (++). It increments the iterator to the next node and returns a copy of the iterator before the increment.

Returns
A copy of the iterator before the increment.

◆ operator--() [1/2]

template<typename T>
Iterator & frequency_list< T >::Iterator::operator-- ( )
inline

Pre-increment – operator for the Iterator class. This operator overloads the pre-increment operator (–). It increments the iterator to the next node and returns a reference to the updated iterator.

Returns
Iterator&

◆ operator--() [2/2]

template<typename T>
Iterator frequency_list< T >::Iterator::operator-- ( int )
inline

Post-increment –operator for the Iterator class. This operator overloads the post-increment operator (–). It increments the iterator to the next node and returns a copy of the iterator before the increment.

Returns
Iterator

◆ operator=()

template<typename T>
Iterator & frequency_list< T >::Iterator::operator= ( const std::shared_ptr< node > & ptr)
inline

Assignment operator for the Iterator class.

It assigns the value of the given shared pointer to the current node of the iterator and returns a reference to the updated iterator.

Parameters
ptrThe shared pointer to assign.
Returns
A reference to the updated iterator.

◆ operator==()

template<typename T>
bool frequency_list< T >::Iterator::operator== ( const Iterator & it)
inline

Equality operator for the Iterator class.

This operator checks if two iterators are equal by comparing their current nodes.

Parameters
itThe other Iterator object to compare with.
Returns
true if the iterators are equal, false otherwise.

◆ push_back()

void frequency_list< T >::push_back ( T data)
inline

Adds an element to the back of the frequency list.

This function adds an element to the back of the frequency list. If the frequency list is empty, it creates a new node with the given data and frequency of 1 and sets it as the head. If the element already exists in the frequency list, it increments its frequency. Otherwise, it creates a new node with the given data and frequency of 1, and appends it to the end of the frequency list.

Parameters
dataThe data to be added to the frequency list.

◆ push_front()

void frequency_list< T >::push_front ( T data)
inline

Adds an element to the front of the frequency list.

If the frequency list is not empty, it updates the previous pointer of the current head node to the new node. Then it sets the new node as the new head of the frequency list.

Parameters
dataThe data to be added to the frequency list.

◆ search()

bool frequency_list< T >::search ( T key)
inline

Searches for a given key in the frequency list.

This function searches for a given key in the frequency list. If the key is found, it increments the frequency of the corresponding node and returns true. If the key is not found, it returns false.

Parameters
keyThe key to search for in the frequency list.
Returns
Returns true if the key is found and the frequency is incremented, false otherwise.

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const frequency_list< T > & flist )
friend

Overloaded output stream insertion operator for the frequency_list class.

This operator allows the frequency_list object to be printed to the output stream. It iterates through the frequency_list and outputs each element along with its frequency in the format: element(frequency)

Parameters
osThe output stream to write to.
flistThe frequency_list object to be printed.
Returns
The output stream.

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