Learn C++
#include "stlcontainers/list_deque.hpp"

list & deque in C++

//concept

std::list is a doubly-linked list: O(1) insertion and erasure anywhere given an iterator, but O(n) indexed access (no operator[] at all) and pointer-chasing cache behavior. std::deque (double-ended queue) is the middle ground — O(1) push/pop at both ends with chunked storage that's friendlier to caches than list.

Practical rule: vector wins by default; deque wins for queues needing both-end growth; list wins only when iterator stability across inserts plus mid-container splicing genuinely matters — rarer than textbooks suggest.

//how it works
1

list: nodes and iterators

Elements live in scattered nodes; insert/erase at a known iterator is O(1) and never invalidates other iterators.

2

No indexing on list

Reaching element k walks k links (O(n)) — if you index often, you picked the wrong container.

3

deque: both ends fast

push_front and push_back are both O(1), and operator[] exists — the natural double-ended buffer.

4

Choose by access pattern

Random access → vector; both-end growth → deque; stable iterators plus splicing → list.

//example
#include <iostream>
#include <deque>
#include <list>
 
int main() {
std::deque<int> d;
d.push_back(2);
d.push_front(1);
std::list<int> l{1, 2, 3};
l.push_front(0);
std::cout << d[0] << d[1] << ", " << l.size() << std::endl;
return 0;
}
Output
12, 4
1push_back(2) then push_front(1) build the deque as [1, 2] — growth at either end is O(1).
2The list gains 0 at its front in O(1) with zero invalidation of other iterators, reaching size 4.
3d[0]d[1] print 12 (deque supports indexing) beside the list's size 4.
//use cases
Sliding windows and double-ended work queues with deque
Splice-heavy algorithms moving ranges between lists in O(1)
Undo histories with cheap push_front and stable iterators
Defaulting back to vector when profiling shows list's cache misses hurt

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself — building beats watching.