C Program to Implement a Singly Linked List

What is Singly Linked List?

Linked List is a collection of interconnected nodes that together represent a sequence. Connected node means if you have access of a node, then you can get access to the next nodes even though they are not located in consecutive memory locations.

For singly linked list, you can traverse the list only in one direction. The diagram below shows an example of a singly linked list.

Linked List

It has four data nodes. Each node has an integer as value and a link to the next node. Linked list head always points to the first node and the last node always points to NULL. We can traverse this list from head to the last node but not in other direction. That’s why it is called singly linked list.

C Data Structure for Singly Linked List

We’ll start with the basic building block of linked list.

struct node{
    int val;
    struct node * next;
};

struct node *head = NULL;

A linked list node is represented by a C structure (struct) with two parts, 1) value or data and 2) link. We defined the value part as integer here but it can be arbitrarily complex. The link (next) is a pointer that points to the same data structure. It holds the next node pointer. But the last node next pointer is always NULL,

The head pointer always points the first node. Initially it is assigned with NULL but it points point to the first node if one or more nodes are present in the list.

C Program

This C program is an example to implement a linked list.

#include <stdio.h>
#include <stdlib.h>

struct node{
    int val;
    struct node *next;
};

/*Print the linked list*/
void print_list(struct node *head)
{
    printf("H->");

    while(head)
    {
        printf("%d->", head->val);
        head = head->next;
    }

    printf("|||\n");
}

/*Insert an element at the front of the list*/
void insert_front(struct node **head, int value)
{
    struct node * new_node = NULL;

    /*Allocating memory for the new node*/
    new_node = (struct node *)malloc(sizeof(struct node));

    if (new_node == NULL)
    {
        printf("Failed to insert element. Out of memory");
    }

    new_node->val = value;

    /*Pointing the new node to where head is currently pointing to*/
    new_node->next = *head;

    /*Pointing head to new node.*/
    *head = new_node;
}

void main()
{
    int count = 0, i, val;
    struct node * head = NULL;

    printf("Enter number of elements: ");
    scanf("%d", &count);

    for (i = 0; i < count; i++)
    {
        printf("Enter %dth element: ", i);
        scanf("%d", &val);
        insert_front(&head, val);
    }

    printf("Linked List: ");
    print_list(head);
}

This program has two important functions.

  1. insert_front(): It creates a new node and inserts that node at the front of the list. This function takes the head of the list and a value as input. It first creates a new node and sets the input as value of that node. Then it points the new node next pointer to where the head is pointing to. And then it points head to the new node.
  2. print_list(): It prints the nodes of a linked list. It takes the head as input and traverse the list. At every iteration it prints the value of the current node.

The main() function takes integers as input and inserts them into the linked list using insert_front() function. At the end, it prints the whole list using print_list() function.

Here is the output.

Enter number of elements: 4
Enter 0th element: 41
Enter 1th element: 53
Enter 2th element: 56
Enter 3th element: 76

Linked List: H->76->56->53->41->|||

Read also: Insert an element into a linked list at any position.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

43
29
21
26
214
36