Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Type Conversion C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs Challenge C Nested Structures C Structs & Pointers C Unions C typedef C Struct Padding

C Enums

C Enums

C Memory

C Memory Management

C Errors

C Errors C Error Challenge C Debugging C NULL C Error Handling C Input Validation

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers

C DSA

C DSA Intro C Searching C Sorting C Stacks C Queues C Linked Lists C Trees C Big O

C Projects

C Projects

C Cert

C Certificate

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A

C DSA Linked Lists

Share

What is a Linked List?

A linked list stores items in nodes. Each node holds two things:

  • data - the value
  • next - a pointer to the next node

The first node is called the head. The last node points to NULL, which means "this is the end of the list".

A linked list. The head points to a node with 10. Its next pointer points to a node with 20, which points to a node with 30. The last node points to NULL. 10 20 30 NULL head data next

Linked Lists vs. Arrays

A linked list is similar to an array, but it stores its values in a different way.

Both store many values. But they are good at different things:

Array Linked List
Size Fixed when you create it Grows and shrinks while the program runs
In memory Items are side by side Items can be anywhere, connected by pointers
Get item number 5 Fast: jump straight to it Slower: start at the head and follow the pointers
Add an item at the start Slower: move all the other items Fast: change one pointer

Create a Node

In C, a node is a structure with a value and a pointer:

Example

struct Node {
  int data;
  struct Node *next;
};

A structure that points to its own type might look strange at first. It just means: "a node knows where the next node is".


Link Nodes Together

Let's create three nodes and link them. Then we "walk" through the list and print each value:

Example

#include <stdio.h>

struct Node {
  int data;
  struct Node *next;
};

int main() {
  // Create three nodes
  struct Node a, b, c;
  a.data = 10;
  b.data = 20;
  c.data = 30;

  // Link them together
  a.next = &b;
  b.next = &c;
  c.next = NULL; // The last node points to NULL

  // Walk through the list and print each value
  struct Node *current = &a;
  while (current != NULL) {
    printf("%d -> ", current->data);
    current = current->next;
  }
  printf("NULL\n");

  return 0;
}

Result:

10 -> 20 -> 30 -> NULL
Try it Yourself »

Example Explained

  • &b is the memory address of node b. So a.next = &b; makes a point to b.
  • current is a pointer that starts at the first node.
  • current->data reads the value of the node that current points to (see Structs and Pointers).
  • current = current->next; moves to the next node. When current is NULL, the loop stops.

Visiting every node, one by one, is called traversing the list.



Insert a Node in the Middle

Here is where linked lists shine. To put a new node between two others, you only change two pointers. Nothing else moves!

We also put the printing loop in a function, printList(), so we can use it again:

Example

#include <stdio.h>

struct Node {
  int data;
  struct Node *next;
};

void printList(struct Node *head) {
  struct Node *current = head;
  while (current != NULL) {
    printf("%d -> ", current->data);
    current = current->next;
  }
  printf("NULL\n");
}

int main() {
  struct Node a, b, c;
  a.data = 10;
  b.data = 20;
  c.data = 30;
  a.next = &b;
  b.next = &c;
  c.next = NULL;

  printList(&a);

  // Put a new node (15) between 10 and 20
  struct Node d;
  d.data = 15;
  d.next = &b; // 15 points to 20
  a.next = &d; // 10 points to 15

  printList(&a);

  return 0;
}

Result:

10 -> 20 -> 30 -> NULL
10 -> 15 -> 20 -> 30 -> NULL
Try it Yourself »

With an array, you would have to move 20 and 30 one step to the right to make room. With a linked list, you just "hook in" the new node.

Run the simulation below to see how the code above works, one step at a time. The green arrow is the pointer that just changed:


Summary

  • A linked list stores items in nodes. Each node has a value and a pointer to the next node.
  • The first node is the head. The last node points to NULL.
  • To visit every node, start at the head and follow the next pointers.
  • Adding a node only changes pointers. No other items need to move.

So far, our nodes are fixed variables in main(). In the next chapter, you will create nodes while the program runs, and learn to insert and delete them.


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.