-
Hajipur, Bihar, 844101
In C++, a List is part of the Standard Template Library (STL) and represents a doubly linked list. Unlike arrays or vectors that store elements in contiguous memory, a list stores each element in separate memory blocks linked together by pointers. This allows fast insertion and deletion of elements anywhere in the sequence without the need to shift other elements.
Lists are best suited when your program needs to frequently add or remove data from the middle of the sequence, rather than perform random access operations.
A C++ List provides flexibility that arrays or vectors cannot offer efficiently. Here are the main benefits of using a list:
Dynamic Size: A list grows and shrinks dynamically as elements are added or removed.
Fast Insertions/Deletions: Since elements are linked, no shifting occurs, making insertions and deletions faster.
Bidirectional Traversal: You can move both forward and backward through elements using iterators.
Efficient Memory Usage: Elements are allocated separately, so there’s no need for contiguous memory.
However, lists also have some limitations. Random access is not supported, meaning you cannot access an element directly by index like list[i].
To use a list, include the <list> header file. You can declare and initialize it like this:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> numbers = {10, 20, 30, 40};
for (int num : numbers)
cout << num << " ";
return 0;
}
Explanation:
Here, we declared a list named numbers containing four integer elements. The program uses a range-based for loop to display all elements.
The STL List provides a variety of functions to manipulate its elements. Let’s go through some of the most useful ones.
You can add elements to a list using push_back() and push_front().
list<int> nums;
nums.push_back(10);
nums.push_front(5);
push_back() adds an element at the end.
push_front() adds an element at the beginning.
Lists allow you to remove elements easily using the following methods:
nums.pop_back(); // Removes last element
nums.pop_front(); // Removes first element
nums.remove(10); // Removes all occurrences of 10
Since lists don’t support direct indexing, we use iterators to access elements:
for (auto it = nums.begin(); it != nums.end(); ++it)
cout << *it << " ";
Here, begin() returns an iterator pointing to the first element, and end() points just after the last element. Using *it, we access the value stored at the iterator.
Lists have built-in functions for sorting and reversing elements.
nums.sort(); // Sort elements in ascending order
nums.reverse(); // Reverse the order
These operations are efficient since they work internally with node pointers rather than moving elements in memory.
You can merge two sorted lists using the merge() function and remove duplicate elements using unique().
list<int> list1 = {1, 2, 3};
list<int> list2 = {4, 5, 6};
list1.merge(list2);
list1.unique();
After merging, list1 will contain all elements from both lists, and list2 becomes empty.
Here’s a complete example that demonstrates various list operations:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> values = {40, 10, 30, 20};
values.push_front(5);
values.push_back(50);
values.sort();
values.reverse();
cout << "List elements: ";
for (int val : values)
cout << val << " ";
return 0;
}
Output:
List elements: 50 40 30 20 10 5
Efficient insertions and deletions.
Supports bidirectional iteration.
Can easily handle large dynamic data sets.
No reallocation required like vectors.
Slower access time compared to arrays or vectors.
Uses more memory due to pointer storage.
No random access using indexes.
Use lists when:
You frequently insert or delete elements in the middle of the sequence.
You don’t require random access.
Memory fragmentation is not a concern.
The C++ List is a flexible and efficient container in the STL used for sequential data where frequent insertions and deletions are required. While it doesn’t provide random access, it compensates with quick modifications and smooth traversal. Understanding when to use lists over vectors or arrays can help in writing optimized and maintainable C++ programs.
Write a C++ program to create a list of integers and insert elements using push_back() and push_front().
Create a program that removes specific elements from a list using the remove() function.
Write a program to sort and reverse a list of integers using sort() and reverse().
Build a program that merges two sorted lists and displays the merged list.
Write a C++ program that removes duplicate elements from a list using the unique() function.
Create a program that demonstrates bidirectional traversal of a list using iterators.
Write a program to insert a new element before a specific position using an iterator.
Build a program that finds the minimum and maximum elements in a list manually without using STL algorithms.
Write a program that reads 5 strings from the user, stores them in a list, and displays them in alphabetical order.
Create a C++ program that clears a list using clear() and checks if it’s empty using empty().