The key distinction between them and forward list objects is that the former are single-linked lists, which means that they can only be iterated forwards in exchange for being a little bit smaller and more effective.”
C++ List
As a result, a vector does not keep its elements in contiguous memory as a list does. Since it takes a long time to move all the information, insertion and deletion in the middle of a vector are quite expensive. Linked list, which uses a list container in its implementation, solves this issue. List saves the elements on a contiguous memory, but vector stores on a non-contiguous memory. As it takes a long time to move all the elements, insertion and deletion in the vector are quite expensive.
Lists provide bidirectional communication and offer an effective means of performing insertion and deletion operations.
Types of the List
Single list
It is the simplest kind of linked list, with each node including data and a pointer to the node after it that has the same type of data. Since it has a pointer to the following node, the node stores the address of that node in the sequence. Data can only be traversed one way in a single linked list.
Double link list
The data and additional pointer, known as the prior pointer that is present in a singly linked list, are also present in a doubly linked list. The first node’s preceding link in the list points to NULL, just like the end node of the list’s next node does.
Circular double list
A circular linked list has all of its nodes connected in a circle. The final NULL is absent. There are two types of circular linked lists: single circular and double circular.
Benefits of C++ List
Since the linked list’s size fluctuates while it is being used, there is no memory loss.
There is no memory loss because the linked list’s size changes as it is used.
- Implementation: Linear data structures like stacks and queues are frequently implemented using connected lists.
- Insertion and deletion services: The connected list greatly simplifies insertion and deletion. There is no need to relocate an element after it has been added or removed; instead, the address in the next pointer just needs to be altered.
C++ List Functions
List::front()
The built-in C++ STL function list::front returns a reference to the first entry in a list container.
Syntax:
Parameters:
This function merely returns a reference to the first entry in the list container; it does not accept any parameters.
Return Value: The first element in the list container is directly referenced by this function.
Exception: When used with an empty list container, this function generates an undefined behavior.
List back()
A direct reference to the final element in the list container is returned by the list::back() method in the C++ STL. Because list::end() only delivers an iterator to the last entry, this function differs from that one.
Syntax:
Parameters:
There is no parameter; return the value. The final element in the list container demo list is directly referenced by this function.
Exception: There is no such exception in this method, but invoking it with an empty list container causes C++ to behave in an undefinable way.
List::push_front()
The built-in C++ STL method push front() is used to insert an element right before the top element in a list container. Additionally, the container’s size is boosted by this function by 1.
Syntax:
Parameters:
This option designates the element that must be added to the top of the list.
Return Value:
Nothing is returned by this function.
List:push_back()
To insert a new element into an existing list container, use the list:push back() function in the C++ STL. It adds the element to be added to the list container by taking it as an argument.
Syntax:
Parameters:
There is only one necessary parameter for this function. This refers to the list name element that has to be inserted.
Return Type: This function’s return type is void, and it has no return value.
List::pop_front()
A list container’s front element can be removed using the built-in C++ STL function list::pop front(). As a result, this function reduces the size of the container by 1 while removing the entry from the list’s top.
Syntax:
Return Value:
Nothing is returned by this function.
List::pop_back()
Use the built-in C++ STL method list::pop back to remove an element from a list container’s back. In other words, the last item in a list container is removed by this function. Removing a member from the list’s end this function reduces the container’s size by 1.
Syntax:
Return Value:
Nothing is returned by this function.
List::begin()
This distinguishes it from the front() function.
Syntax:
Parameters:
No parameters are passed.
List::rend()
The built-in list::rend() method in the C++ STL returns a reverse iterator that points to a location before the list’s start.
Syntax:
Return value:
A reverse iterator points to a location before the list’s start is returned.
List::cbegin()
The built-in C++ STL method cbegin() returns a constant random access iterator that points to the start of the list.
Syntax:
List::cend()
A constant random access iterator that points to the end of the list is returned by the built-in C++ STL method cend().
Syntax:
List::crbegin()
The built-in C++ STL method crbegin() returns a constant reverse iterator that leads to the list’s final element or the container’s reversed beginning. Due to the constant nature of the iterator, the elements cannot be altered or changed.
Syntax:
List::crend()
The built-in C++ STL method crend returns a constant reverse iterator linked to the #hypothetical element that comes before the first element in the list or the reverse end of the list. The iterator is constant; therefore, the elements cannot be changed or updated.
Syntax:
List::empty()
To check whether a certain list container is empty or not, use the built-in C++ STL function list::empty(). This function doesn’t alter the list; it simply determines if a list is empty or not or whether its size is 0 or not.
Syntax:
List::insert()
To insert entries into a list at any point, use the list::insert() function. Position the number of elements to insert and the value to insert are the three parameters for this function. When not specified, the number of elements is set to one by default.
Syntax:
List::erase()
The built-in C++ STL function erase() is used to remove items from a list container. The supplied list container can be used to remove a single element or a group of elements using this function.
Syntax:
Parameters:
We pass two parameters.
Position: When a single element is to be deleted using the function, this parameter is utilized. This parameter refers to an iterator that identifies the element from the list container that has to be deleted.
First, last: The terms “parameter first” and “parameter last” refer to the iterator pointing to the first and last elements in the range, respectively, that need to be deleted. This eliminates all of the range’s elements, including the element that the iterator pointed to first but excluding the element that the iterator pointed to last.
List::assign()
To assign values to a list, use the built-in C++ STL method assign().
Syntax:
Parameters:
Count: The number of values required to be added to the list name.
Value: Starting with the initial element, this value will be assigned a certain number of times. If the list already has some elements, the element supplied in the parameter value will take its place. This parameter’s data type must be the same as the list name’s data type.
List::remove()
It eliminates elements that evaluate against a value. It takes a value as an input and removes any items from the list container whose values match the value passed in as the function’s parameter.
Syntax:
List::remove()
The remove() method is used to eliminate any entries from a list that match a value specified as a function parameter.
Syntax:
List::remove_if()
The remove if() function is used to eliminate all entries from a list that satisfy a condition or predicate that is sent in as an argument.
Syntax:
List::reverse()
A list container can be reversed using the built-in method reverse() in the C++ STL. It changes the order of the list of container items.
Syntax:
List::size()
In other words, it is employed to determine the list container’s size.
Syntax:
List::resize()
It adjusts the list container’s size to fit exactly “n” elements when given the number “n” as a parameter.
Syntax:
List::sort()
The container’s elements can be sorted by shifting them around using the sort() function.
Syntax:
List::max_size()
Max size() returns the most elements a list container can accommodate.
Syntax:
List::unique()
Eliminates all consecutive members from the list that are duplicates. It only functions with sorted lists.
Syntax:
List::swap()
By using this function, the contents of two lists of the same size and type can be swapped.
Syntax:
List::clear()
The list container’s size is reduced to 0 when all the elements are removed using the clear() function.
Syntax:
List::operator=
Using this operator, the container’s existing contents are replaced and given new assignments. Additionally, it adjusts the size to fit the new content.
Syntax:
In this article, we will explain different illustrations of the List functions.
Example no 1
Here we are going to see how List functions work.
#include <iostream>
#include <list>
int main() {
std::listmy_list = { 50, 10, 30 };
for (int y :my_list) {
std::cout<< y << '\n';
}
}
In this code, we incorporate header files<iostream>, <list>, and <algorithm>. Then we call the main() function. Let’s start the coding in the main() function. In the main() function, add my list, and pass the different values. Utilize the “For” loop and initialize the “y” variable and add the value of the “my_list” variable.
Example no 2
In this instance, we see how to decrease the value of the iterator by using List functions.
#include <list>
using namespace std;
int main(void) {
listl;
list l0 = { 0, 0, 0 };
list l9(l0.begin(), l0.end());
list l15(move(l0));
cout<< "Size of list 0: " << l0.size() <<endl;
cout<< "List 9 contents: " <<endl;
for (auto it = l9.begin(); it != l9.end(); ++it)
cout<< *it <<endl;
cout<< "List 15 contents: " <<endl;
for (auto it = l15.begin(); it != l15.end(); ++it)
cout<< *it <<endl;
return 0;
}
Here, we add header files<iostream>, <list>. <list> header file deals with the use of C++ list functions. Then we will be using namespace. Along with this, we called the main() function. First, we initialized the list; then, we passed the values. We add begin() function to get the 9 elements from it. Next, we add move(), 15 elements from this move() function. We have been using “cout” to print the statement “size of list 0”. And the next “cout” prints the statement “list 15 contents”. In the end, we utilize the “for” loop, and then the value of the iterator decreases after all this “return0” command is entered.
We acquire this type of outcome after running the aforementioned code.
Conclusion
First, we talked about C++ list functions. Then we observe several C++ functions apply to the list. We defined different C++ list functions and also their syntax. In this article, we have run various codes that contain C++ list functions. In the first illustration, we use a linked list and pass the parameters. In the second example, we apply the functions list and pass the parameters in every function.