Brief Overview on Linked List
This documentation is written in a way to introduce the idea of a linked list
 
Loading...
Searching...
No Matches
Linked_list Class Reference

Linked List Class with Methods and Properties. More...

#include <linked_list.hpp>

Public Member Functions

 Linked_list ()
 Constructor Creates an new empty Linked List object.
 
 Linked_list (std::vector< int > lst_values)
 Constructor Creates a linked list object from the values stored in the vector object.
 
 ~Linked_list ()
 Destructor Removes all the nodes stored in the Linked list when the Linked List object is terminated.
 
double front () const
 Method will return the value stored in the node at the front of the linked list (head node) if not empty.
 
bool empty () const
 Method will return a boolean value indicating if the linked list is empty.
 
void push_front (int new_value)
 Method will create a new node with the assigned value and placed it at the front of linked list.
 
void pop_front ()
 Method will remove the current head node and replace it with the next node in the list, if there is any.
 
void clear ()
 Method will clear all the nodes in the linked list, leaving an empty linked list object.
 

Friends

std::ostream & operator<< (std::ostream &out, Linked_list const &lst)
 Allows this function to access the private member variables.
 

Detailed Description

Linked List Class with Methods and Properties.

Constructor & Destructor Documentation

◆ Linked_list() [1/2]

Linked_list::Linked_list ( )

Constructor Creates an new empty Linked List object.

◆ Linked_list() [2/2]

Linked_list::Linked_list ( std::vector< int > lst_values)

Constructor Creates a linked list object from the values stored in the vector object.

Each value will be assigned a node, and all the nodes will form the linked list. The formation of the linked list object will maintain the same order as seen in the vector object.

Parameters
lst_valuesThe values to assign the nodes.

◆ ~Linked_list()

Linked_list::~Linked_list ( )

Destructor Removes all the nodes stored in the Linked list when the Linked List object is terminated.

Member Function Documentation

◆ clear()

void Linked_list::clear ( )

Method will clear all the nodes in the linked list, leaving an empty linked list object.

Example

Linked_list lst = Linked_list( std:vector<int>( 10, 1 ) );
std::cout << lst.empty() << std::endl;
lst.clear();
std::cout << lst.empty() << std::endl;
Linked List Class with Methods and Properties.
Definition linked_list.hpp:15
bool empty() const
Method will return a boolean value indicating if the linked list is empty.
Definition linked_list.cpp:41
Linked_list()
Constructor Creates an new empty Linked List object.
Definition linked_list.cpp:10
void clear()
Method will clear all the nodes in the linked list, leaving an empty linked list object.
Definition linked_list.cpp:58

Output:

0
1

◆ empty()

bool Linked_list::empty ( ) const

Method will return a boolean value indicating if the linked list is empty.

Returns
The status of the linked list being empty

Example 1

Linked_list lst = Linked_list( std::vector<int>( 1, 1 ) );
std::cout << lst.empty() << std::endl;

Output:

0

Example 2

std::cout << lst.empty() << std::endl;

Output:

1

◆ front()

double Linked_list::front ( ) const

Method will return the value stored in the node at the front of the linked list (head node) if not empty.

Otherwise, it will throw an Out of Range Error

Returns
The value stored in the head node.

Example

Linked_list lst = Linked_list( std::vector<int>( 3, 1 ) );
std::cout << lst.front() << std::endl;
double front() const
Method will return the value stored in the node at the front of the linked list (head node) if not em...
Definition linked_list.cpp:33

Output:

1

◆ pop_front()

void Linked_list::pop_front ( )

Method will remove the current head node and replace it with the next node in the list, if there is any.

If the linked list is empty, nothing will happen

Example

lst.push_front( 3 );
lst.push_front( 4 );
std::cout << lst.front() << std::endl;
lst.pop_front();
std::cout << lst.front() << std::endl;
void pop_front()
Method will remove the current head node and replace it with the next node in the list,...
Definition linked_list.cpp:49
void push_front(int new_value)
Method will create a new node with the assigned value and placed it at the front of linked list.
Definition linked_list.cpp:45

Output:

4
3

◆ push_front()

void Linked_list::push_front ( int new_value)

Method will create a new node with the assigned value and placed it at the front of linked list.

Parameters
new_valueThe value of the node you want to assign as the new head node

Example

Linked_list lst = Linked_list( std::vector<int>( 3, 2) );
std::cout << lst.front() << std::endl;
lst.push_front( 4 );
std::cout << lst.front() << std::endl;

Output:

2
4

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & out,
Linked_list const & lst )
friend

Allows this function to access the private member variables.

Parameters
outThe output data
lstThe Linked list object

Example

Linked_list lst = Linked_list( std::vector<int>( 4, 5 ) );
std::cout << lst << std::endl;

Output:

HEAD -> 5 -> 5 -> 5 -> 5 -> NULLPTR

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