A list in C++ is a sequence container that allows you to store elements one after another.
- Implemented as a doubly linked list and maintains both front and back for fast operations on both the ends.
- Data is stored in non-contiguous memory, allowing fast insertion and deletion anywhere in the list (beginning, middle, or end).
#include <iostream>
#include <list>
using namespace std;
int main(){
list<int> myList;
myList.push_back(10);
myList.push_back(20);
myList.push_front(5);
cout << "List elements: ";
for (int n : myList){
cout << n << " ";
}
cout << endl;
return 0;
}
Output
List elements: 5 10 20
Syntax
A list is defined as the std::list class template inside the <list> header file.
list<T> l;
where,
- T: Type of elements in the list.
- l: Name assigned to the list.
Basic Operations
The basic operations of list are shown below:
1. Inserting Elements
- insert() is used for fast insertion if the position iterator is known; otherwise, traverse the list to reach the position.
- push_front() is used to insert at the beginning and push_back() to insert at the end.
#include <iostream>
#include <list>
using namespace std;
int main(){
list<int> l = {3, 2};
l.push_back(5);
l.push_front(1);
auto it = l.begin();
advance(it, 2);
l.insert(it, 4);
for (auto i : l)
cout << i << " ";
return 0;
}
Output
1 3 4 2 5
2. Accessing Elements
- Lists do not allow random access, so to get an element at a specific position, you need to go through the list one by one from the start or end.
- The first and last elements can be accessed quickly using front() and back() methods.
#include <iostream>
#include <list>
using namespace std;
int main(){
list<int> l = {1, 3, 4, 2, 5};
cout << l.front() << endl;
cout << l.back() << endl;
cout << *next(l.begin(), 2);
return 0;
}
Output
1 5 4
Explanation: "next(l.begin(), 2)" moves the iterator two positions forward from the start of the list and * dereferences it to print the element at index 2 (the third element).
3. Updating Elements
- List elements can be updated by accessing them with an iterator and using the assignment operator (=) to set a new value.
- Since lists do not support random access, you must use an iterator to reach the element you want to update.
#include <iostream>
#include <list>
using namespace std;
int main(){
list<int> l = {1, 3, 4, 2, 5};
l.front() = 11;
auto it = l.begin();
advance(it, 2);
*it = 10;
for (auto i : l)
cout << i << " ";
return 0;
}
Output
11 3 10 2 5
4. Finding Elements
- To find an element in a list, you can use the find() function from the <algorithm> library.
- find() returns an iterator to the element if it is found, or the end iterator if it is not found.
#include <iostream>
#include <list>
using namespace std;
int main(){
list<int> l = {1, 3, 4, 2, 5};
// Finding 4
auto it = find(l.begin(), l.end(), 4);
if (it != l.end())
cout << *it;
else
cout << "Element Not Found!";
return 0;
}
Output
4
5. Traversing
- A list can be traversed using begin() and end() iterators in a loop.
- Start from begin() and keep moving the iterator until it reaches end(), accessing each element along the way.
#include <iostream>
#include <list>
using namespace std;
int main(){
list<int> l = {1, 3, 4, 2, 5};
// Traversing using iterators
for (auto it = l.begin(); it != l.end(); ++it)
cout << *it << " ";
return 0;
}
Output
1 3 4 2 5
6. Deleting Elements
- erase() deletes an element from the list using an iterator to its position.
- pop_front() and pop_back() quickly delete the first and last elements of the list.
#include <iostream>
#include <list>
using namespace std;
int main(){
list<int> l = {1, 3, 4, 2, 5};
l.pop_back();
l.pop_front();
auto it = l.begin();
advance(it, 2);
l.erase(it);
for (auto i : l)
cout << i << " ";
return 0;
}
Output
3 4
Other Member Functions of List
Following is the list of all member functions of std::list class in C++:
Functions | Description |
|---|---|
| Returns the number of elements in the list. | |
| Checks if the list is empty. | |
| Returns a reverse iterator pointing to the last element of the list. | |
Returns a reverse iterator pointing to the element before the first element. | |
| Removes all elements from the list. |
Forward List vs List
Feature | forward_list | list |
|---|---|---|
Type of linked List | Singly linked list | Doubly linked list |
Traversal | Can only traverse forward | Can traverse both forward and backward |
Memory usage | Uses less memory (only one pointer per node) | Uses more memory (two pointers per node) |
Insertion/Deletion | Fast insertion and deletion, but only at or after a given position | Fast insertion and deletion anywhere (before or after a position) |
Functions supported | Limited compared to list (no size(), no reverse iterators) | More complete interface including size(), reverse(), bidirectional iterators. |