Lists in C++ are part of the Standard Template Library (STL). A list is a sequence container that stores elements in a non-contiguous way using a doubly linked list structure.
What is a List?
A list is a container that stores elements where each element is linked to the previous and next element. Unlike arrays or vectors, lists do not use continuous memory.
Why Use Lists?
Lists are useful because they:
- Allow fast insertion and deletion
- Do not require shifting elements
- Use dynamic memory efficiently
- Work well with frequent modifications
- Do not need continuous memory allocation
List Header File
To use lists in C++, include the <list> library.
#include <list>
Declaring a List
list<int> numbers;
Initializing a List
list<int> numbers = {10, 20, 30};
Adding Elements in List
Lists allow insertion at both ends.
Example
numbers.push_back(40); // Add at end
numbers.push_front(5); // Add at beginning
Removing Elements from List
Example
numbers.pop_back(); // Remove last element
numbers.pop_front(); // Remove first element
Example Program of List
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> numbers = {1, 2, 3};
numbers.push_front(0);
numbers.push_back(4);
for (int num : numbers) {
cout << num << endl;
}
return 0;
}
Output
0
1
2
3
4
Accessing Elements in List
Lists do not support direct indexing like vectors or arrays.
You must use iterators.
Example
for (auto it = numbers.begin(); it != numbers.end(); it++) {
cout << *it << endl;
}
Common List Functions
| Function | Purpose |
|---|---|
push_back() | Add element at end |
push_front() | Add element at beginning |
pop_back() | Remove last element |
pop_front() | Remove first element |
size() | Get number of elements |
clear() | Remove all elements |
empty() | Check if list is empty |
Iterator Example with List
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> numbers = {10, 20, 30};
for (auto it = numbers.begin(); it != numbers.end(); it++) {
cout << *it << endl;
}
return 0;
}
Difference Between List and Vector
| Vector | List |
|---|---|
| Contiguous memory | Non-contiguous memory |
| Fast random access | No random access |
| Slower insertion in middle | Fast insertion/deletion |
| Better for indexing | Better for modifications |
Important Points About Lists
- Implemented as doubly linked list
- No direct indexing support
- Fast insertion and deletion
- Uses more memory than vector
Advantages of Lists
- Efficient insertion and deletion
- Dynamic memory usage
- No need for shifting elements
- Flexible structure
Common Mistakes with Lists
- Trying to use indexing (
numbers[0]) - Forgetting to use iterators
- Ignoring memory overhead
- Using list when vector is more suitable
Real-Life Example
Think of a chain of connected train compartments:
- Each compartment is linked to next and previous
- You can easily add or remove compartments
- No fixed structure required
This is similar to how a list works.
Applications of Lists
Lists are used in:
- Task scheduling systems
- Undo/redo operations
- Memory management systems
- Real-time data processing
- Complex data structures
Why Lists are Important
Lists are important because they:
- Handle dynamic data efficiently
- Allow fast modifications
- Support advanced data structures
- Improve flexibility in programming
Conclusion
Lists in C++ are powerful STL containers based on doubly linked lists. They are best suited for applications that require frequent insertion and deletion. Although they do not support direct indexing, they provide high flexibility and efficiency for dynamic data handling.