Dremendo Tag Line

Singly Linked List in C Programming

Data Structure in C

In this lesson, we will understand what is Singly Linked List in C Programming and how to create them along with some examples.

What is Singly Linked List in C

A Singly Linked List in C is a data structure in which data is store in the form of structure called a node. Each node has two parts, the data and the pointer part. The data part stores the data, and the pointer part stores the memory address of the next node, as shown in the below image.

c singly linked list

In the above image, start is a structure pointer that stores the memory address of the first node.

The first node has two parts, the data part and the pointer part next. The next part stores the memory address of the next node.

In a singly linked list data, are not stored in a contagious memory location. A new node can be created anywhere in the memory at runtime.

We can create more nodes as we want by linking the newly created node with the last node.

video-poster

Structure of a Singly Linked List Program

Below we have given the complete structure of a singly linked list program in C along with all the operation that we are going to perform on it.

Program

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

// Define node structure
typedef struct node
{
    int data;
    struct node *next;
} node;

int main()
{
    node *start=NULL,*newnode,*nextnode,*rn,*pn;
    int ch,n,x,c,f;

    for(;;)		// An infinite loop
    {
        system("cls");		// for clearing the screen
        printf("1. Add\n");
        printf("2. Display\n");
        printf("3. Insert Before\n");
        printf("4. Insert After\n");
        printf("5. Count\n");
        printf("6. Search\n");
        printf("7. Delete\n");
        printf("8. Reverse\n");
        printf("Enter Choice: ");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:		// Create new node
                break;

            case 2:		// Display all nodes
                break;

            case 3:		// Insert node before
                break;

            case 4:		// Insert node after
                break;

            case 5:		// Count total nodes
                break;

            case 6:		// Search a node
                break;

            case 7:		// Delete a node
                break;

            case 8:		// Reverse list
                break;

            default:	// default case for wrong choice input
                printf("Wrong Choice");
                getch();	// pause the loop to see the message
        }
    }
    return 0;
}

The above program shows how a singly linked list program will look with all the operations on it. Now we will discuss each part of the above program and the program required to be written in each case for each individual operations like create, insert before, insert after and so on.

Define Node Structure

So first of all we will define a node structure above the main() function that will store data and the address of the next node, as shown below.

Example

typedef struct node
{
    int data;
    struct node *next;
} node;

In the above code, node is the name of a structure data type. It contains an integer variable data to store the number and a node type pointer variable *next to store the memory address of another node.

Declare Variables

After defining the node structure, now we will declare the necessary variables required for our program as shown below

Example

node *start=NULL, *newnode, *nextnode, *rn, *pn;
int ch, n, x, c, f;

The description of the above variables are as follows.

  • *start will store the address of the first node in the memory. The default value is NULL.
  • *newnode will store the address of newly created node.
  • *nextnode will store the address of next node after the current node. This variable will be used to reverse the linked list.
  • *rn will store the address of recent node. This variable will be used when we will read each node one by one.
  • *pn will store the address of the previous node (node before the recent node).
  • ch will store the choice of the menu input by the user like 1, 2, 3, etc.
  • n will store the number input by the user and will be used later on in the program for various purposes.
  • x will store the number input by the user and will be used later on in the program for various purposes.
  • c will be used to count the total number of nodes present in the linked list.
  • f will be used to check if a node is found or not. When a node is found in the list the value of f will be 1 otherwise 0.

Add Operation (case 1:)

In the add operation, we will add a new node in the linked list using the program given below.

case 1:

printf("Enter a number ");
scanf("%d",&n);

// create a new node
newnode=(node*)malloc(sizeof(node));
newnode->data=n;         // assign the value of n in the data part of newnode
newnode->next=NULL;      // assign the value NULL in the next part of newnode

if(start==NULL)     // if start is NULL
{
    start=newnode;		// then assing the newnode in start (start points to the first node in the memory)
}
else
{
    rn=start;	// if start is not NULL then assign start in rn to start reading from the first node

    // run a while loop until if we find NULL in the next part of rn
    while(rn->next!=NULL)
    {
        rn=rn->next;     // if rn->next part is not NULL then move to the next node
    }
    rn->next=newnode;    // when while loop ends, the rn stands at the last node and we assign the newnode in the next part of the rn
}
break;

Display Operation (case 2:)

In the display operation, we will display all the nodes on the screen using the program given below.

case 2:

if(start==NULL)
{
    printf("List is empty");
    getch();	// pause the loop to see the message
}
else
{
    rn=start;	// recent node

    // display the nodes on the screen
    while(rn!=NULL)
    {
        printf("%d ",rn->data);
        rn=rn->next;
    }
    getch(); // pause the loop to see the nodes
}
break;

Insert Before Operation (case 3:)

In the insert before operation, we will insert a new node before a selected node in the linked list using the program given below.

case 3:

f=0;
if(start==NULL)
{
    printf("List is empty");
    getch();	// pause the loop to see the message
}
else
{
    printf("Insert Number ");
    scanf("%d",&n);
    printf("Insert Before ");
    scanf("%d",&x);

    // Create a new node
    newnode=(node*)malloc(sizeof(node));
    newnode->data=n;
    newnode->next=NULL;

    // search number x in the list from the starting node
    rn=start;
    while(rn!=NULL)
    {
        if(start==rn && rn->data==x)     // insert before first node
        {
            newnode->next=rn;	// newnode->next = recent node
            start=newnode;		// start = new node
            f=1;
            break;
        }
        else if(start!=rn && rn->data==x)    // insert before recent node
        {
            newnode->next=pn->next;	    // newnode->next = previous node->next
            pn->next=newnode;		    // previous node->next = newnode
            f=1;
            break;
        }
        pn=rn;      // previous node = recent node
        rn=rn->next;     // recent node = recent node->next
    }

    if(f==0)
    {
        printf("Number not found");

        // delete the newely created node
        free(newnode);

        getch();	// pause the loop to see the message
    }
}
break;

Insert After Operation (case 4:)

In the insert after operation, we will insert a new node after a selected node in the linked list using the program given below.

case 4:

f=0;
if(start==NULL)
{
    printf("List is empty");
    getch();	// pause the loop to see the message
}
else
{
    printf("Insert Number ");
    scanf("%d",&n);
    printf("Insert After ");
    scanf("%d",&x);

    // Create a new node
    newnode=(node*)malloc(sizeof(node));
    newnode->data=n;
    newnode->next=NULL;

    // search number x in the list from the starting node
    rn=start;
    while(rn!=NULL)
    {
        if(rn->data==x)
        {
            newnode->next=rn->next;	    // newnode->next = recent node->next
            rn->next=newnode;		    // recent node->next = newnode
            f=1;
            break;
        }
        rn=rn->next;		// recent node = recent node->next
    }

    if(f==0)
    {
        printf("Number not found");

        // delete the newely created node
        free(newnode);

        getch();	// pause the loop to see the message
    }
}
break;

Count Operation (case 5:)

In the count operation, we will count the total numbers of nodes present in the linked list using the program given below.

case 5:

c=0;
if(start==NULL)
{
    printf("List is empty");
    getch();	// pause the loop to see the message
}
else
{
    // count from the first node till last node
    rn=start;
    while(rn!=NULL)
    {
        c=c+1;
        rn=rn->next;		// recent node = recent node->next
    }
    printf("Total Nodes = %d",c);
    getch();	// pause the loop to see the message
}
break;

Search Operation (case 6:)

In the search operation, we will search if a given node present in the linked list using the program given below.

case 6:

f=0;
c=0;
if(start==NULL)
{
    printf("List is empty");
    getch();	// pause the loop to see the message
}
else
{
    printf("Enter Number to Search ");
    scanf("%d",&x);

    // search number x in the list from the starting node
    rn=start;
    while(rn!=NULL)
    {
        c=c+1;			// count the numbers
        if(rn->data==x)
        {
            printf("Number %d Found at Position %d",rn->data,c);
            f=1;
            break;
        }
        rn=rn->next;		// recent node = recent node->next
    }

    if(f==0)
    {
        printf("Number not found");
    }
    getch();	// pause the loop to see the message
}
break;

Delete Operation (case 7:)

In the delete operation, we will delete a given node from the linked list using the program given below.

case 7:

f=0;
if(start==NULL)
{
    printf("List is empty");
    getch();	// pause the loop to see the message
}
else
{
    printf("Enter Number to Delete ");
    scanf("%d",&x);

    // search number x in the list from the starting node
    rn=start;
    while(rn!=NULL)
    {
        if(start==rn && rn->data==x)
        {
            // delete the first node
            start=rn->next;		// start = recent node->next
            free(rn);			// delete the recent node
            f=1;
            printf("Number Deleted Successfully");
            break;
        }
        else if(start!=rn && rn->data==x)
        {
            // delete the recent node
            pn->next=rn->next;	// previous node->next = recent node->next
            free(rn);			// delete the recent node
            f=1;
            printf("Number Deleted Successfully");
            break;
        }
        pn=rn;			// previous node = recent node
        rn=rn->next;		// recent node = recent node->next
    }

    if(f==0)
    {
        printf("Number not found");
    }
    getch();	// pause the loop to see the message
}
break;

Reverse Operation (case 8:)

In the reverse operation, we will reverse the reading order of nodes in the linked list by changing their pointer direction using the program given below.

case 8:

if(start==NULL)
{
    printf("List is empty");
    getch();	// pause the loop to see the message
}
else
{

    rn=start;		// recent node = start
    pn=NULL;		// previous node = NULL

    while(rn!=NULL)
    {
        nextnode=rn->next;		// nextnode = recent node->next
        rn->next=pn;			// recent node->next = previous node
        pn=rn;					// previous node = recent node
        rn=nextnode;			// recent node = nextnode
    }
    start=pn;

    // print the reverse list
    rn=start;
    while(rn!=NULL)
    {
        printf("%d ",rn->data);
        rn=rn->next;
    }
    getch();	// pause the loop to see the nodes
}
break;