The STL list
in C++ is a doubly linked list, meaning each node has two pointers (next and previous). Unlike vector
and deque
, it does not support random access (list[i]
is not allowed) but provides fast insertions and deletions at any position.
✅ Key Features:
✔ Efficient Insertions/Deletions anywhere (O(1) at the front, back, or middle).
✔ No Random Access – You must use iterators to traverse elements.
✔ Dynamic Size – No need to predefine size.
✔ Bidirectional Iterators – Supports forward and reverse traversal.
1️⃣ Syntax & Declaration
#include <iostream>
#include <list> // Include list library
using namespace std;
int main() {
list<int> myList = {10, 20, 30, 40, 50}; // Declare and initialize
cout << "First Element: " << myList.front() << endl;
cout << "Last Element: " << myList.back() << endl;
return 0;
}
🔹 Output
First Element: 10
Last Element: 50
✅ Unlike vector
, list
does not allow direct indexing! 🚀
2️⃣ Adding & Removing Elements
🔹 Adding Elements
list<int> myList;
myList.push_back(10); // Add at the back
myList.push_front(20); // Add at the front
🔹 Removing Elements
myList.pop_back(); // Removes last element
myList.pop_front(); // Removes first element
✅ Unlike vector
, list
supports push_front()
and pop_front()
.
3️⃣ Traversing a list
🔹 Using an Iterator
for (auto it = myList.begin(); it != myList.end(); it++) {
cout << *it << " ";
}
🔹 Using a Range-Based Loop
for (int val : myList) {
cout << val << " ";
}
🔹 Using Reverse Iterator
for (auto it = myList.rbegin(); it != myList.rend(); it++) {
cout << *it << " ";
}
✅ Since list
has no direct access (list[i]
is not allowed), iterators are used for traversal!
4️⃣ Common list
Functions
🔹 Size & Capacity
cout << "Size: " << myList.size() << endl;
🔹 Accessing Elements
cout << myList.front(); // First element
cout << myList.back(); // Last element
🔹 Checking if Empty
if (myList.empty()) cout << "List is empty";
🔹 Insert at Specific Position
auto it = myList.begin();
advance(it, 2); // Move iterator to 3rd position
myList.insert(it, 15); // Insert 15 at index 2
🔹 Erase an Element
auto it = myList.begin();
advance(it, 2); // Move iterator to 3rd position
myList.erase(it); // Removes element at index 2
✅ Fast insertions & deletions in the middle compared to vector
! 🚀
5️⃣ Sorting a list
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> myList = {50, 10, 40, 20, 30};
myList.sort(); // Sort in ascending order
cout << "Sorted List: ";
for (int val : myList) cout << val << " ";
return 0;
}
🔹 Output
Sorted List: 10 20 30 40 50
✅ Uses sort()
function of list
(no need for algorithm
library).
6️⃣ Merging Two Lists
list<int> list1 = {10, 20, 30};
list<int> list2 = {15, 25, 35};
list1.merge(list2); // Merges list2 into list1 (both must be sorted)
✅ Merges two sorted lists into one sorted list!
7️⃣ Reversing a list
myList.reverse();
✅ Reverses the list elements efficiently!
8️⃣ Swapping Two list
s
list<int> list1 = {1, 2, 3};
list<int> list2 = {7, 8, 9};
list1.swap(list2); // Swap contents
cout << "After swapping:\n";
for (int val : list1) cout << val << " "; // 7 8 9
cout << endl;
for (int val : list2) cout << val << " "; // 1 2 3
✅ swap()
exchanges contents efficiently.
9️⃣ Removing Specific Elements
myList.remove(20); // Removes all occurrences of 20
✅ Works like erase()
, but removes by value instead of iterator.
🔟 Clearing a list
myList.clear(); // Removes all elements
✅ Memory remains allocated. To free memory, use:
list<int>().swap(myList);
📌 Summary
✔ std::list<T>
– Doubly linked list (fast insertions & deletions).
✔ Supports push_back()
, push_front()
, pop_back()
, pop_front()
.
✔ Efficient insertions/deletions in the middle (better than vector
).
✔ No direct access (list[i]
not allowed), use iterators instead.
✔ Sorting via sort()
, merging via merge()
, reversing via reverse()
.
✔ Resizable with clear()
, swap()
, remove()
.
Would you like an STL stack
next? 🚀
0 Comments