Linked list creation and traversal is the stepping stone in data structures. In this article, I will explain how to create and traverse a linked list in C programming. I will explain step by step process to create and traverse a linked list of n nodes and display its elements.
Write a C program to implement Singly linked list data structure. C program to create a linked list and display elements of linked list.

Required knowledge
Functions, Structures, Pointers, Dynamic Memory Allocation
How to create and traverse a linked list?
In previous article we discussed about singly linked list data structure, its need and advantages. Here we will learn to create and traverse a linked list in C program.
How to create a linked list?
Step by step descriptive logic to create a linked list.
- The first step of creating linked list of n nodes starts from defining node structure. We need a custom type to store our data and location of next linked node. Let us define our custom node structure
Where data is the data you want to store in list. *next is pointer to the same structure type. The *next will store location of next node if exists otherwise
NULL
.Note: The node structure may vary based on your requirement. You can also have user defined types as node data section.
- Declare a pointer to node type variable to store link of first node of linked list. Say
struct node *head;
.Note: You can also declare variable of node type along with node structure definition.
- Input number of nodes to create from user, store it in some variable say n.
- Declare two more helper variable of node type, say
struct node *newNode, *temp;
. - If
n > 0
then, create our first node i.e. head node. Use dynamic memory allocation to allocate memory for a node. Sayhead = (struct node*)malloc(sizeof(struct node));
. - If there is no memory to allocate for head node i.e.
head == NULL
. Then print some error message and terminate program, otherwise move to below step. - Input data from user and assign to head using
head->data = data;
. - At first head node points to
NULL
. Hence, assignhead->next = NULL;
. - Now, we are done with head node we should move to creation of other nodes. Copy reference of head to some other temporary variable, say
temp = head;
. We will use temp to store reference of previous node. - Allocate memory and assign memory reference to newNode, say
newNode = (struct node*)malloc(sizeof(node));
. - If memory got allocated successfully then read data from user and assign to data section of new node. Say
newNode->data = data;
. - Make sure new node points to
NULL
. - Now link previous node with newly created node i.e.
temp->next = newNode;
. - Make current node as previous node using
temp = temp->next;
. - Repeat step 10-14 for remaining
n - 2
other nodes.
How to traverse a linked list?
Step by step descriptive logic to traverse a linked list.
- Create a temporary variable for traversing. Assign reference of head node to it, say
temp = head
. - Repeat below step till
temp != NULL
. temp->data
contains the current node data. You can print it or can perform some calculation on it.- Once done, move to next node using
temp = temp->next;
. - Go back to 2nd step.
Example program to create and traverse a linked list
Enter the total number of nodes: 5 Enter the data of node 1: 10 Enter the data of node 2: 20 Enter the data of node 3: 30 Enter the data of node 4: 40 Enter the data of node 5: 50 Data in the list Data = 10 Data = 20 Data = 30 Data = 40 Data = 50
Happy coding