C++ is a versatile general-purpose programming language that can accomplish almost any task on a machine. This article will teach you C++ from the ground up, from the fundamentals of programming (taught in C++) to advanced concepts such as pointers, classes, templates, and more. 

What is the C++ List?

A list is a contiguous container, while a vector is a non-contiguous container. This means that a list stores its elements in contiguous memory, while a vector does not. Insertion and deletion in the center of a vector are very expensive since moving all the details takes a long time. This problem is solved by Linklist, which is implemented using a list container.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Lists are bidirectional and provide an effective way of inserting and deleting objects. List traversal is slow since it accesses list elements sequentially, whereas vector access is random.

#include<iostream> 

#include<list> 

using namespace std;  

int main() 

   list<int> l; 

Passing the List Statement With Parameters

#include<iostream> 

#include<list> 

using namespace std; 

int main() 

   list<int> l{1,2,3,4}; 

}

You can initialize the list in the following two ways. 

list<int>  new_list{1,2,3,4};

or

list<int> new_list = {1,2,3,4};

A linked list is a complex arrangement that includes a "connection" to the structure that contains the things that follow. It's a set of organized structures, not by their physical location in memory (like an array), but by logical links that are stored as part of the structure's information.

Example 1:

#include <algorithm>

#include <iostream>

#include <list>

int main() {

        std::list<int> my_list = { 12, 5, 10, 9 };

        for (int x : my_list) {

                             std::cout << x << '\n';

        }

}

Output

C_Plus_Plus_list 

Another way to gather similar data is to use a related list. A connected list, unlike an array, does not have elements in consecutive memory locations. A linked list is made up of nodes that are linked together by pointers. A related list is depicted in the diagram.

Example 2:

#include <iostream>

#include <list>

using namespace std;

int main(void) {

        list<int> l;

        list<int> l1 = { 10, 20, 30 };

        list<int> l2(l1.begin(), l1.end());

        list<int> l3(move(l1));

        cout << "Size of list l: " << l.size() << endl;

        cout << "List l2 contents: " << endl;

        for (auto it = l2.begin(); it != l2.end(); ++it)

                cout << *it << endl;

        cout << "List l3 contents: " << endl;

        for (auto it = l3.begin(); it != l3.end(); ++it)

                             cout << *it << endl;

        return 0;

}

Output

C_Plus_Plus_List_2

Types of Lists

Now, have a look at the types of lists in C++:

Single List

It's the most basic type of linked list, with each node containing data and a pointer to the next node with the same data type. The node stores the address of the next node in the sequence since it has a pointer to the next node. A single linked list only allows data to be traversed in one direction.

A Doubly Linked List

A doubly linked list, also known as a two-way linked list, is a more complicated form of a linked list, containing a pointer to both the next and previous node in the chain. Data, a pointer to the next node, and a pointer to the previous node make up the three components. This also allows one to go back through the list.

A Circular Linked List

The circular linked list is a form of a linked list that is circular. The pointer to the first node of the list is included in the last node of a circular linked list. When traversing a circular linked list, you can start at any node and traverse the list in any direction forward or backward until you reach the same node from which you began.

A Circular Doubly Linked List

A circular two-way linked list, also known as a circular doubly linked list, is a more complex type of linked list containing a pointer to both the next and previous node in the chain. The difference between a singly linked list and a circular linked list is the same as the difference between a doubly linked and a circular doubly list.

Advantages of the List in C++

  • A linked list is a dynamic data structure that can expand and shrink in size at runtime by allocating and deallocating memory. As a result, there is no need to specify the linked list's initial size.
  • There is no memory wastage in the linked list because the size of the linked list increases or decreases at run time. Hence there is no memory wastage and no need to pre-allocate memory.
  • Implementation: A connected list is often used to implement linear data structures such as stacks and queues.
  • Service of insertion and deletion: The connected list makes insertion and deletion far simpler. After an element is inserted or deleted, there is no need to shift it; the address in the next pointer only needs to be changed.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

C++ List Functions, Along With Their Use:

Method

Description

insert()

Inserts a new element before the specified position as pointed by the iterator.

push_back()

Adds a new element at the end of the vector.

push_front()

Adds a new element at the front-end of the vector

pop_back()

Deletes the last element.

pop_front()

Deletes the first element.

empty()

Checks if the list is empty.

size()

Finds and returns the number of elements present in the list.

max_size()

Finds and returns the maximum size of the list.

front()

Returns the first element of the list.

back()

Returns the last element of the list.

swap()

swaps two lists when both lists are the same.

reverse()

Reverses the list’s elements.

sort()

Sorts the elements in increasing order.

merge()

Merges two sorted lists.

splice()

Inserts a new list into the list that invokes

unique()

Removes all duplicate elements from the list.

resize()

Changes the size of the list container.

assign()

Assigns a new element to the list container.

emplace()

Inserts a new element at the specified position.

emplace_back()

Inserts a new element at the end of the vector.

emplace_front()

Inserts a new element at the list’s beginning

Conclusion

This was all about lists in C++. If you want to learn and become a well-trained and professional developer, you must enroll in Simplilearn's Post Graduate Program In Full Stack Web Development that teaches you the language from scratch. This will help you in getting the best knowledge along with the right skills. You can get recruited to the best companies with the help of the skills you will learn.

A full-stack developer has a good grasp of the language and an excellent base for AI, graphics, game programming, and general software engineering. And, you will get a clear picture of the same, with this course!

Happy learning!!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449