An adjacency list is a data structure that stores graphs in memory by storing a list of neighbors for each node. Adjacency lists are suitable when there are many nodes but not many connections. It is easy to add edges but costly to remove edges. When the graph is dense (i.e. has many connections), it is better to use an adjacency matrix.

Example of adjacency list in C++:

#include <vector>
#include <algorithm> // std::remove
 
constexpr int N = 100; // nr of nodes
 
// adjacency list for N nodes
std::vector<int> adj[N];
 
void connect(int i, int j) {
    adj[i].push_back(j);
    adj[j].push_back(i);
}
 
void disconnect(int i, int j) {
    // std::remove moves the matching element(s) to the end of the array
    // and returns an iterator to the first matching element, and vector::erase
    // removes these elements.
    adj[i].erase(std::remove(adj[i].begin(), adj[i].end(), j), adj[i].end());
    adj[j].erase(std::remove(adj[j].begin(), adj[j].end(), i), adj[j].end());
}