Table of contents
1.
Introduction
2.
Lists in c++
3.
Implementation
4.
Member functions for the list
5.
FAQs
6.
Key takeaways-
Last Updated: Mar 27, 2024
Easy

List in STL

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will learn about the list in CPP STL. We will start with a brief introduction of the list, then we will learn about the syntax to use, we will go through an example to develop a better understanding of the whole concept. Finally, we will learn about the various methods and functions we can use with them. 

So let's dive straight into the blog and start with the introduction.

Lists in c++

Lists in c++ are containers that store the elements in a linear order like arrays, but it doesn't allow contiguous memory allocation, unlike arrays.

The elements of the lists are called nodes. Each element In the list contains its value and a pointer to the next element, and another to the previous element. This whole set of values of the element and pointer to the next and previous elements is called a node. 

Since these elements are connected to each other only using the pointers and stored in non-contiguous memory locations, we lose the functionality of random access of the elements using indices. Thus to access any element in the list, we will have to start with the head of the linked list then pass through each element to reach the desired element. Thus we can say that the list has slow traversals as compared to the arrays or vectors. 

Normally, lists refer to the Doubly Linked List, and for singly-linked lists, we use a forward list, which we will learn in some other blog. The only major difference in both of them is that we can only travel in one direction in a singly linked list, whereas in a doubly-linked list, we can traverse in both forward and backward directions.

Implementation

Let’s see an example to learn how to use a list and the various methods related to it-

To declare a list, we use the following syntax.

list<typename> list_name;

// CPP program to depict the implementation of a list
#include <bits/stdc++.h>
using namespace std;
// function for printing the elements in a list
void printlist(list<int> l)
{
list<int>::iterator itr;
for (itr = l.begin(); itr != l.end(); ++itr)
cout << *itr<<" ";
cout<<endl;
}
// Driver Code
int main()
{
list<int> testlist;
for (int i = 0; i < 8; i++) {
testlist.push_back(i * 3);
testlist.push_front(i * 4);
}
cout << "\nList is : ";
printlist(testlist);
cout << "\nlist.front() : " << testlist.front();
cout << "\nlist.pop_front() : ";
testlist.pop_front();
printlist(testlist);
cout << "\nlist.pop_back() : ";
testlist.pop_back();
printlist(testlist);
cout << "\list.reverse() : ";
testlist.reverse();
printlist(testlist);
cout << "\nlist.sort(): ";
testlist.sort();
printlist(testlist);
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output-

List is : 28 24 20 16 12 8 4 0 0 3 6 9 12 15 18 21 

list.front() : 28

list.pop_front() : 24 20 16 12 8 4 0 0 3 6 9 12 15 18 21 

list.pop_back() : 24 20 16 12 8 4 0 0 3 6 9 12 15 18 

list.reverse() : 18 15 12 9 6 3 0 0 4 8 12 16 20 24 

list.sort(): 0 0 3 4 6 8 9 12 12 15 16 18 20 24

Let's take a look at a quick video of the implementation before moving on to further C++ STL lists methods and functions.

Member functions for the list

Now it’s time to learn about the various member functions of the list in the CPP STL.

There are several member functions we can use along with the list. Here we will learn about them one by one.

We will first begin with the member function, which returns iterators to various elements-

  • begin- This function returns an iterator to the first element of the list.
  • end- This function returns an iterator to the next position of the last element of the list.
  • rbegin- This function returns a reverse iterator, the last element of the list.
  • rend- This function returns a reverse iterator to the previous location to the first element of the list.
  • cbegin- This function returns const_iterator to the first element of the list 
  • cend- This function returns const_iterator to the next position of the last element of the list.
  • crbegin- This function returns a const_reverse_iterator to reverse the beginning of the list.
  • crend- This function returns a const_reverse_iterator to the reverse end of the list.

Now, let’s see some member functions which are used to do various operations related to the size of the container. 

  • empty- This function tests whether the container is empty.
  • size- This function returns the size of the container.
  • max_size- This function returns the maximum size of the container.

Now, let's see some member functions which act as modifiers in the list.

  • assign- This member function is used to assign new content to the container.
  • emplace_front- This member function is used to construct and insert elements at the beginning of the list.
  • push_front- This member function is used to insert elements at the beginning of the list.
  • pop_front- This member function is used to delete the first element of the list.
  • emplace_back- This member function is used to construct and insert elements at the end of the list.
  • push_back- This member function is used to add elements at the end of the linked list.
  • pop_back- This member function is used to delete the last element.
  • emplace- This member function is used to construct and insert elements.
  • Insert- This member function is used to insert elements into the list.
  • erase- This member function is used to erase elements from the linked list.
  • swap- This member function is used to swap the content of two elements in the list.
  • resize- This member function is used to change the size of the list.
  • clear- This member function is used to clear the content of the list.

Lastly, we will learn about some member functions which are used to operate on the list.

  • merge- Merge sorted lists 
  • sort- This is used to sort elements in the container.
  • reverse- This is used to Reverse the order of elements in the list.

With this, we wrap up our blog and move on to the FAQs for the list.

Check out this problem - First And Last Occurrences Of X

Must Read Python List Operations

FAQs

  1. Can we traverse in the backward direction in the list container?
    Yes, we can traverse the list in a backward direction because the list is a doubly-linked list, in which all the elements have a pointer to their next and the previous element.
  2. Do we need to specify the size of the linked list?
    No, since the linked list is a dynamic data structure, we don't need to specify its size in advance.
  3. What is the difference between the singly linked list and the doubly linked list?
    Each node in the singly linked list contains a pointer to its next element only, whereas, in a doubly-linked list, each element contains a pointer to both its previous element and the next element.
  4. Why can't we randomly access the elements in a list?
    Random accessing of the elements works on the logic that if all the elements of a container are stored sequentially in the memory in a contiguous manner, then we can directly jump to the memory location of an element and access it. 
  5. Mention different types of linked lists?
    There are mainly four types of linked lists-
    Singly-linked lists
    Doubly-linked lists
    Circular-linked lists
    Doubly circular linked lists.

Key takeaways-

In this article, we have extensively discussed the list container in C++ STL.’ We also saw its implementation and discussed its various member functions.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass