Skip to main content

Data Structures Reference

All common data structures across C++, Java, Python, and C# — syntax only, no algorithms.


1. Array (Static)

Fixed-size, contiguous memory. O(1) access by index.

#include <array>

int arr[5] = {1, 2, 3, 4, 5}; // C-style array
std::array<int, 5> arr2 = {1,2,3,4,5}; // std::array (fixed size)

arr[0]; // access
arr[0] = 10; // update

2. Dynamic Array (List / Vector)

Resizable array. Amortized O(1) append.

#include <vector>

std::vector<int> v = {1, 2, 3};
v.push_back(4); // add to end
v.pop_back(); // remove from end
v.insert(v.begin() + 1, 99); // insert at index
v.erase(v.begin() + 1); // remove at index
v.size(); // size
v[0]; // access
v.front(); // first element
v.back(); // last element
v.empty(); // check if empty
v.clear(); // remove all

3. Stack (LIFO)

Last In, First Out. O(1) push/pop.

#include <stack>

std::stack<int> st;
st.push(1); // push
st.push(2);
st.top(); // peek top → 2
st.pop(); // remove top
st.size(); // size
st.empty(); // check if empty

4. Queue (FIFO)

First In, First Out. O(1) enqueue/dequeue.

#include <queue>

std::queue<int> q;
q.push(1); // enqueue
q.push(2);
q.front(); // peek front → 1
q.back(); // peek back → 2
q.pop(); // dequeue (removes front)
q.size(); // size
q.empty(); // check if empty

5. Deque (Double-Ended Queue)

Insert/remove from both ends in O(1).

#include <deque>

std::deque<int> dq;
dq.push_front(1); // add to front
dq.push_back(2); // add to back
dq.front(); // peek front
dq.back(); // peek back
dq.pop_front(); // remove front
dq.pop_back(); // remove back
dq.size();

6. Linked List

Nodes connected by pointers. O(1) insert/delete at known node; O(n) access.

#include <list>

// Singly linked list — manual
struct Node {
int data;
Node* next;
Node(int d) : data(d), next(nullptr) {}
};

// STL doubly linked list
std::list<int> ll = {1, 2, 3};
ll.push_front(0); // add to front
ll.push_back(4); // add to back
ll.pop_front(); // remove front
ll.pop_back(); // remove back
ll.front(); // peek front
ll.back(); // peek back
ll.size();

7. Hash Map (Dictionary)

Key-value pairs. Average O(1) get/put.

#include <unordered_map>  // O(1) avg
#include <map> // O(log n), sorted

std::unordered_map<string, int> mp;
mp["alice"] = 25; // insert / update
mp["bob"] = 30;
mp["alice"]; // access → 25
mp.count("alice"); // check existence → 1
mp.erase("alice"); // delete
mp.size();

for (auto& [key, val] : mp)
cout << key << ": " << val;

8. Hash Set

Unique elements. Average O(1) add/remove/search.

#include <unordered_set>  // O(1) avg
#include <set> // O(log n), sorted

std::unordered_set<int> st;
st.insert(1); // add
st.insert(2);
st.count(1); // check existence → 1
st.erase(1); // remove
st.size();

for (int x : st) cout << x;

9. Priority Queue (Heap)

Min or max element always at top. O(log n) insert, O(1) peek, O(log n) remove.

#include <queue>

// Max-heap (default)
std::priority_queue<int> maxPQ;
maxPQ.push(3);
maxPQ.push(1);
maxPQ.push(5);
maxPQ.top(); // 5 (max)
maxPQ.pop(); // removes 5

// Min-heap
std::priority_queue<int, vector<int>, greater<int>> minPQ;
minPQ.push(3);
minPQ.top(); // smallest

10. Tree Map / Sorted Map

Key-value pairs with keys kept in sorted order. O(log n) all operations.

#include <map>

std::map<int, string> mp;
mp[1] = "one";
mp[3] = "three";
mp[2] = "two";

mp.begin()->first; // smallest key
mp.rbegin()->first; // largest key
mp.lower_bound(2); // iterator to first key >= 2
mp.upper_bound(2); // iterator to first key > 2

11. Sorted Set / Tree Set

Unique elements in sorted order. O(log n) all operations.

#include <set>

std::set<int> st;
st.insert(3);
st.insert(1);
st.insert(5);

*st.begin(); // smallest → 1
*st.rbegin(); // largest → 5
st.lower_bound(2); // iterator to first >= 2
st.upper_bound(3); // iterator to first > 3
st.erase(3);

12. Graph Representations

#include <vector>
#include <unordered_map>

int n = 5; // number of nodes

// Adjacency List (unweighted)
vector<vector<int>> adj(n);
adj[0].push_back(1);
adj[0].push_back(2);

// Adjacency List (weighted)
vector<vector<pair<int,int>>> wadj(n);
wadj[0].push_back({1, 10}); // node 1, weight 10

// Adjacency Matrix
vector<vector<int>> mat(n, vector<int>(n, 0));
mat[0][1] = 1;

// Edge List
vector<pair<int,int>> edges;
edges.push_back({0, 1});

13. Binary Tree Node

struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);

14. Trie (Prefix Tree)

struct TrieNode {
TrieNode* children[26] = {};
bool isEnd = false;
};

struct Trie {
TrieNode* root = new TrieNode();

void insert(string word) {
TrieNode* node = root;
for (char c : word) {
int i = c - 'a';
if (!node->children[i])
node->children[i] = new TrieNode();
node = node->children[i];
}
node->isEnd = true;
}

bool search(string word) {
TrieNode* node = root;
for (char c : word) {
int i = c - 'a';
if (!node->children[i]) return false;
node = node->children[i];
}
return node->isEnd;
}
};

Quick Reference Table

Data StructureC++JavaPythonC#
Dynamic Arrayvector<T>ArrayList<T>listList<T>
Stackstack<T>ArrayDeque<T>listStack<T>
Queuequeue<T>ArrayDeque<T>dequeQueue<T>
Dequedeque<T>ArrayDeque<T>dequeLinkedList<T>
Linked Listlist<T>LinkedList<T>dequeLinkedList<T>
Hash Mapunordered_mapHashMapdictDictionary
Hash Setunordered_setHashSetsetHashSet
Sorted MapmapTreeMapSortedDictSortedDictionary
Sorted SetsetTreeSetSortedListSortedSet
Min-Heappriority_queue (greater)PriorityQueueheapqPriorityQueue
Max-Heappriority_queuePriorityQueue (reversed)heapq (negated)PriorityQueue