Singly Linked List in C

Singly Linked List in C

Singly Linked List in C are one  of the simplest linear data structure, that we use for storing our data in a  easy and efficient way.  Linked List in C comprises of nodes like structures, which can further be divided into 2 parts in the case of singly linked list. These two parts are-:

  • Node – for storing the data.
  • Pointer – for storing the address of the next node.

In a Singly linked list there is only one pointer type variable, that contains the address of the next node.

Singly Linked List

Implementation of Singly Linked List in C

We implement Linked List  using user defined data type, with the help of structure or struct. Since Singly linked list has only 1 pointer type value, which means it can store the address of only one node, which will be  the next to it. As a result, we can only move in one direction in a Singly Linked List i.e; from head to tail, which makes traversing a linked list more difficult. Implementing  linked list  rather than using array is better, since the insertion and deletion operations in  Linked List are less complicated and less time consuming in comparison with arrays.

Singly Linked Lists in C

How to Construct a Singly Linked List in C ?

For constructing a singly linked list in C we make use of structure keyword(struct), for creating user defined data types, which can store various different types of data in the nodes of the  singly linked list. Each linked list has two parts, one for storing the desired data and the other is a pointer type variable, which stores the address of the next node.

Syntax for creating a node

struct Node {
  int Data;
  Struct Node* next;
};

This code will create a data type Node, which  will be able to store two values-:

  1. int value – data
  2. pointer value – address of the next node

Insertion of a node

void insert(struct Node** head, int data){
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));

  newNode->data = data;
  newNode->next = *head;

 
 //changing the new head to this freshly entered node
  *head = newNode; 
}
Insertion of node in Singly Linked List in C

Deletion of a node

void delete()  
    {  
        struct node *ptr,*ptr1;  
        if(head == NULL)  
        {  
            printf("\nlist is empty");  
        }  
        else if(head -> next == NULL)  
        {  
            head = NULL;  
            free(head);  
            printf("\nOnly node of the list deleted ...");  
        }  
              
        else  
        {  
            ptr = head;   
            while(ptr->next != NULL)  
                {  
                    ptr1 = ptr;  
                    ptr = ptr ->next;  
                }  
                ptr1->next = NULL;  
                free(ptr);  
                printf("\n Deleted Node from the last ...");  
            }     
        }
Deletion of node in Singly Linked List in C

Code for Implementing Single Linked List in C

    #include  <stdio.h>
    #include  <stdlib.h>
    void insert(int);  
    void delete();  
    struct node  
    {  
        int data;  
        struct node *next;  
    };  
    struct node *head;  
    void main ()  
    {  
        int choice,item;  
        do   
        {  
            printf("\n1.Insert a node\n2.Delete a node\n3.Exit\n4.Enter your choice?");  
            scanf("%d",&choice);  
            switch(choice)  
            {  
                case 1:  
                printf("\nEnter the item\n");  
                scanf("%d",&item);  
                insert(item);  
                break;   
                case 2:  
                delete();  
                break;   
                case 3:  
                exit(0);  
                break;    
                default:  
                printf("\nPlease enter a valid choice\n");  
            }

        }while(choice != 3);  
    }  
    void insert(int item)  
        {  
            struct node *ptr = (struct node *)malloc(sizeof(struct node *));  
            if(ptr == NULL)  
            {  
                printf("\nOVERFLOW\n");  
            }  
            else  
            {  
                ptr->data = item;  
                ptr->next = head;  
                head = ptr;  
                printf("\nNode inserted\n");  
            }  

        }  
    void delete()  
        {  
            struct node *ptr,*ptr1;  
            if(head == NULL)  
            {  
                printf("\nlist is empty");  
            }  
            else if(head -> next == NULL)  
            {  
                head = NULL;  
                free(head);  
                printf("\nOnly node of the list deleted ...");  
            }  

            else  
            {  
                ptr = head;   
                while(ptr->next != NULL)  
                    {  
                        ptr1 = ptr;  
                        ptr = ptr ->next;  
                    }  
                    ptr1->next = NULL;  
                    free(ptr);  
                    printf("\n Deleted Node from the last ...");  
                }     
            }
Linked list in C meme 2