Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

C++ STL: list (Complete Guide)

Divya Jain
Divya Jain

List in STL is another storage container that allows us to store data sequentially. It helps in efficiently inserting and deleting elements bidirectionally. It is implemented as linear doubly LinkedList in memory. Unlike vectors, they allow non-contiguous memory allocation. Lists are included in #include <list> header file.

List Declaration

Syntax:

list<data_type> name{initial_values};

data_type: data type of the elements to be stored in the list

initial_values: optional parameter which initializes the list with the given values

Example:

list < int > l; //initializes a list of size 0 which stores integer values 
list < int > l { 1, 2, 3 } //initializes a list with values 1,2,3

Functions on lists

  • push_back(element): It pushes the value at the end of the list.
    Time Complexity: O(1) 
    Parameters: the element which is to be inserted
    Return type: void
     
  • push_front(element): It adds a new element to the front of the list.
    Time Complexity: O(1) 
    Parameters: the element which is to be inserted
    Return type: void
     
  • pop_back(): It deletes the last element of the list.
    Time Complexity: O(1) 
    Parameters: None
    Return type: void
     
  • pop_front(): It deletes the first element of the list.
    Time Complexity: O(1) 
    Parameters: None
    Return type: void
     
  • front(): It returns the first element of the list.
    Parameters: None
    Return type: the first element of the list
     
  • back(): It returns the last element of the list.
    Parameters: None
    Return type: last element of the list
     
  • begin(): It returns an iterator pointing to the first element of the list.
    Parameters: None
    Return type: iterator
     
  • end(): It returns an iterator pointing to the last element of the list.
    Parameters: None
    Return type: iterator
     
  • clear(): It deletes all the elements from the list.
    Parameters: None
    Return type: void
     
  • empty(): It tells us whether the list is empty or not.
    Parameters: None
    Return type: boolean, true if the list is empty else false
     
  • reverse(): It reverses the list.
    Parameters: None
    Return type: void
     
  • merge(list_name): It merges two sorted lists.
    Parameters: list_name to be merged
    Return type: None
#include<iostream>
#include<list>
using namespace std;

void print(list < int > l) {
  list < int > ::iterator itr;
  for (itr = l.begin(); itr != l.end(); ++itr)
    cout << * itr << ' ';
  cout << '\n';
}

int main() {
  list < int > l;
  for (int i = 0; i < 5; ++i) {
    l.push_back(i);
  }
  print(l);
  cout << "First element " << l.front() << '\n';
  cout << "Last element " << l.back() << '\n';
  l.pop_front();
  print(l);
  l.pop_back();
  print(l);
  return 0;
}

Output

0 1 2 3 4 
First element 0
Last element 4
1 2 3 4 
1 2 3 

Copying one list to another

list < int > list2 = list1; //copies list1 into list2	

Any change in list2 will not affect list1. The time complexity of this operation is O(N) where N is the size of the list1. The same can be done using reference but it will not create a copy, it will contain a reference to the original list.

list < int > & list2 = list1; //list2 references list1

Inserting elements into a list

The insert() function is used to add elements at any position in a list.

Syntax:

insert(pos, no_of_elements, element);

where,

pos: Iterator pointing to the position where the element is to be inserted.

no_of_elements: Number of elements to insert. Default is 1.

element: The element which is to be inserted. 

list < int > ::iterator it = l.begin();
l.insert(it, 5); // inserts 5 at 1st position

Deleting elements from the list

The erase() function is used to delete a single element or a range of elements from a list. 

Syntax:

listName.erase(iterator pos) //to delete a single element

listName.erase(iterator first, iterator last) // to delete a range of elements

where,

pos: Iterator pointing to the position of the element to be deleted.

first: Iterator pointing to the position of the first element in the range to be deleted.

last: Iterator pointing to the position of the last element in the range to be deleted. The last element doesn't get deleted.

list < int > ::iterator itr = l.begin();
l.erase(itr);
itr1 = demoList.begin();
itr2 = demoList.begin();
itr2++;
l.erase(itr1, itr2);
Divya Jain
Divya Jain
Divya is an incoming SDE at Atlassian. She loves solving problems and web development. She loves to explore new things and is up for interesting conversations about tech.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet