C Adjacency List

Intermediate
6 min read
Updated: Nov 2026
1 example

A List of Neighbors per Vertex

An adjacency list keeps, for each vertex, a linked list of its neighbors. Memory is O(V + E), iterating neighbors is proportional to the degree, and edge tests cost O(degree). It is the default choice for sparse graphs.

Remember
struct Edge { int to; struct Edge *next; };
struct Edge *adj[V];
/* add u->v: e->next = adj[u]; adj[u] = e; */

Cheat Sheet

Keep these facts handy while you work with Adjacency List in C.

TopicDetail
MemoryO(V + E)
NeighborsO(degree)
Edge testO(degree)
Best forSparse graphs

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Creating a Graph in C.

Example 1 — Build and print adjacency lists

Each undirected edge is added in both directions.

C
#include <stdio.h>
#include <stdlib.h>
#define V 4

struct Edge { int to; struct Edge *next; };

static void add_edge(struct Edge *adj[], int u, int v) {
    struct Edge *e = malloc(sizeof *e);
    if (!e) exit(1);
    e->to = v;
    e->next = adj[u];
    adj[u] = e;
}

int main(void) {
    struct Edge *adj[V] = {0};
    int edges[][2] = {{0, 1}, {0, 2}, {1, 2}, {2, 3}};

    for (int i = 0; i < 4; i++) {
        add_edge(adj, edges[i][0], edges[i][1]);
        add_edge(adj, edges[i][1], edges[i][0]);
    }

    for (int u = 0; u < V; u++) {
        printf("%d:", u);
        for (struct Edge *e = adj[u]; e; e = e->next) printf(" %d", e->to);
        printf("\n");
    }

    for (int u = 0; u < V; u++) {
        struct Edge *e = adj[u];
        while (e) { struct Edge *n = e->next; free(e); e = n; }
    }
    return 0;
}

How It Works

  1. Head insertion lists the newest neighbor first.
  2. Vertex 2 gets neighbors 0, 1, then 3, which print as 3, 1, 0.
  3. Every list is freed at the end.

Edge Cases & Pitfalls

Check these before relying on Adjacency List in real programs.

leak

Leaking edge nodes

Free every list when the graph is destroyed.

order

Neighbor order

Head insertion reverses insertion order, which changes traversal order.

Key Takeaways

  • Lists of neighbors.
  • Memory O(V + E).
  • Ideal for sparse graphs.

One line: An adjacency list keeps, for each vertex, a linked list of its neighbors.

Frequently Asked Questions

A representation where each vertex has a list of the vertices it is connected to.
It stores only existing edges, using O(V + E) memory instead of O(V^2).
Continue with "Creating a Graph in C" next. Wrap a dynamic adjacency matrix in a struct with add, degree, and free. The C Graphs hub lists every topic in order.

Next: Creating a Graph in C

Wrap a dynamic adjacency matrix in a struct with add, degree, and free.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful