Single Linked List

What is Single Linked List?
 
Simply a list is a sequence of data, and the linked list is a sequence of data linked with each other.
The formal definition of a single linked list is as follows..
Single linked list is a sequence of elements in which every element has link to its next element in the sequence.
 
In any single linked list, the individual element is called as "Node". Every "Node" contains two fields, data field, and the next field. The data field is used to store actual value of the node and next field is used to store the address of next node in the sequence.
The graphical representation of a node in a single linked list is as follows...
 
 

  In a single linked list, the address of the first node is always stored in a reference node known as "front" (Some times it is also known as "head").
  Always next part (reference part) of the last node must be NULL.
 
Example
 

Operations on Single Linked List
 
The following operations are performed on a Single Linked List
  • Node Creation
  • Insertion
  • Deletion
  • Display
 Node Creation
 
     struct node   
     {      
        int data;    
        struct node *next; 
     }; 
     struct node *head, *ptr;  
     ptr = (struct node *)malloc(sizeof(struct node *));  

 
Insertion

In a single linked list, the insertion operation can be performed in three ways. They are as follows...

  1. Inserting At Beginning of the list
  2. Inserting At End of the list
  3. Inserting At Specific location in the list
 
Inserting At Beginning of the list
  
We can use the following steps to insert a new node at beginning of the single linked list...
  • Step 1 - Create a node ptr with given value.
  • Step 2 - Check whether list is Empty (head == NULL)
  • Step 3 - If it is Empty then, set ptr -> next = NULL and head = ptr.
  • Step 4 - If it is Not Empty then, set ptr -> next = head and head = ptr.

 
Inserting At End of the list 
 
We can use the following steps to insert a new node at end of the single linked list...
  • Step 1 - Create a node ptr with given value and ptr -> next as NULL.
  • Step 2 - Check whether list is Empty (head == NULL).
  • Step 3 - If it is Empty then, set head = ptr.
  • Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
  • Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp -> next is equal to NULL).
  • Step 6 - Set temp -> next = ptr and ptr->next = NULL;.

 
Inserting At Specific location in the list (After a Node)
 
We can use the following steps to insert a new node after a node in the single linked list...
  • Step 1 - Create a node ptr with given value.
  • Step 2 - Check whether list is Empty (head == NULL)
  • Step 3 - If it is Empty then, set ptr -> next = NULL and head = ptr.
  • Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
  • Step 5 - Keep moving the temp to its next node until it reaches to the node after which we want to insert the newNode.
  • Step 6 - Every time check whether temp is reached to last node or not. If it is reached to last node then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move the temp to next node.
  • Step 7 - Finally, Set 'ptr -> next = temp -> next' and 'temp -> next = ptr'

 Deletion
  
In a single linked list, the deletion operation can be performed in three ways. They are as follows...
  1. Deleting from Beginning of the list
  2. Deleting from End of the list
  3. Deleting a Specific Node
 
Deleting from Beginning of the list
  
We can use the following steps to delete a node from beginning of the single linked list...
  • Step 1 - Check whether list is Empty (head == NULL)
  • Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
  • Step 3 - If it is Not Empty then, define a Node pointer 'ptr' and initialize with head.
  • Step 4 - Check whether list is having only one node (ptr -> next == NULL)
  • Step 5 - If it is TRUE then set head = NULL and delete ptr.
  • Step 6 - If it is FALSE then set head = ptr -> next, and delete ptr.
 Deleting from End of the list
  
We can use the following steps to delete a node from end of the single linked list...
  • Step 1 - Check whether list is Empty (head == NULL)
  • Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
  • Step 3 - If it is Not Empty then, define two Node pointers 'ptr' and 'ptr1' and initialize 'ptr' with head.
  • Step 4 - Check whether list has only one Node (ptr -> next == NULL)
  • Step 5 - If it is TRUE. Then, set head = NULL and delete ptr. And terminate the function. (Setting Empty list condition)
  • Step 6 - If it is FALSE. Then, set 'ptr1 = ptr ' and move temp to its next node. Repeat the same until it reaches to the last node in the list. (until ptr -> next == NULL)
  • Step 7 - Finally, Set ptr1 - > next = NULL and delete ptr.
Deleting a Specific Node from the list
 
 We can use the following steps to delete a specific node from the single linked list...
  • Step 1 - Check whether list is Empty (head == NULL)
  • Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
  • Step 3 - If it is Not Empty then, define two Node pointers 'ptr' and 'ptr1' and initialize 'ptr' with head.
  • Step 4 - Keep moving the ptr until it reaches to the exact node to be deleted or to the last node. And every time set 'ptr1 = ptr' before moving the 'ptr' to its next node.
  • Step 5 - If it is reached to the last node then display 'Given node not found in the list! Deletion not possible!!!'. And terminate the function.
  • Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node or not
  • Step 7 - If list has only one node and that is the node to be deleted, then set head = NULL and delete ptr (free(ptr)).
  • Step 8 - If list contains multiple nodes, then check whether ptr is the first node in the list (ptr == head).
  • Step 9 - If ptr is the first node then move the head to the next node (head = head -> next) and delete ptr.
  • Step 10 - If ptr is not first node then check whether it is last node in the list (ptr → next == NULL).
  • Step 11 - If ptr is last node then set ptr1 ->  next = NULL and delete ptr (free(ptr)).
  • Step 12 - If ptr is not first node and not last node then set ptr1 -> next = ptr → next and delete ptr (free(ptr)).
Displaying a Single Linked List

We can use the following steps to display the elements of a single linked list...

  • Step 1 - Check whether list is Empty (head == NULL)
  • Step 2 - If it is Empty then, display 'List is Empty!!!' and terminate the function.
  • Step 3 - If it is Not Empty then, define a Node pointer 'ptr' and initialize with head.
  • Step 4 - Keep displaying ptr -> data with an arrow (--->) until ptr reaches to the last node
  • Step 5 - Finally display ptr - > data with arrow pointing to NULL.
Implementation of Single Linked List using C Programming
 
  #include<stdio.h>  
    #include<stdlib.h>  
    struct node   
    {  
        int data;  
        struct node *next;   
    };  
    struct node *head;  
      
    void beginsert ();   
    void lastinsert ();  
    void randominsert();  
    void begin_delete();  
    void last_delete();  
    void random_delete();  
    void display();  
    void main ()  
    {  
        int choice =0;  
        while(choice != 9)   
        {  
            printf("\n\n*********Main Menu*********\n");  
            printf("\nChoose one option from the following list ...\n");  
            printf("\n===============================================\n");  
            printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from Beginning\n5.Delete from last\n6.Delete node after specified location\n7.Show\n8.Exit\n");  
            printf("\nEnter your choice?\n");         
            scanf("\n%d",&choice);  
            switch(choice)  
            {  
                case 1:  
                beginsert();      
                break;  
                case 2:  
                lastinsert();         
                break;  
                case 3:  
                randominsert();       
                break;  
                case 4:  
                begin_delete();       
                break;  
                case 5:  
                last_delete();        
                break;  
                case 6:  
                random_delete();          
                break;  
                case 7:  
                display();        
                break;   
                case 8:  
                exit(0);  
                break;
                default:  
                printf("Please enter valid choice..");  
            }  
        }  
    }  
    void beginsert()  
    {  
        struct node *ptr;  
        int item;  
        ptr = (struct node *) malloc(sizeof(struct node *));  
        if(ptr == NULL)  
        {  
            printf("\nOVERFLOW");  
        }  
        else  
        {  
            printf("\nEnter value\n");    
            scanf("%d",&item);    
            ptr->data = item;  
            ptr->next = head;  
            head = ptr;  
            printf("\nNode inserted");  
        }  
          
    }  
    void lastinsert()  
    {  
        struct node *ptr,*temp;  
        int item;     
        ptr = (struct node*)malloc(sizeof(struct node));      
        if(ptr == NULL)  
        {  
            printf("\nOVERFLOW");     
        }  
        else  
        {  
            printf("\nEnter value?\n");  
            scanf("%d",&item);  
            ptr->data = item;  
            if(head == NULL)  
            {  
                ptr -> next = NULL;  
                head = ptr;  
                printf("\nNode inserted");  
            }  
            else  
            {  
                temp = head;  
                while (temp -> next != NULL)  
                {  
                    temp = temp -> next;  
                }  
                temp->next = ptr;  
                ptr->next = NULL;  
                printf("\nNode inserted");  
              
            }  
        }  
    }  
    void randominsert()  
    {  
        int i,loc,item;   
        struct node *ptr, *temp;  
        ptr = (struct node *) malloc (sizeof(struct node));  
        if(ptr == NULL)  
        {  
            printf("\nOVERFLOW");  
        }  
        else  
        {  
            printf("\nEnter element value");  
            scanf("%d",&item);  
            ptr->data = item;  
            printf("\nEnter the location after which you want to insert ");  
            scanf("\n%d",&loc);  
            temp=head;  
            for(i=0;i<loc;i++)  
            {  
                temp = temp->next;  
                if(temp == NULL)  
                {  
                    printf("\ncan't insert\n");  
                    return;  
                }  
              
            }  
            ptr ->next = temp ->next;   
            temp ->next = ptr;   
            printf("\nNode inserted");  
        }  
    }  
    void begin_delete()  
    {  
        struct node *ptr;  
        if(head == NULL)  
        {  
            printf("\nList is empty\n");  
        }  
        else   
        {  
            ptr = head;  
            head = ptr->next;  
            free(ptr);  
            printf("\nNode deleted from the begining ...\n");  
        }  
    }  
    void last_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 ...\n");  
        }  
              
        else  
        {  
            ptr = head;   
            while(ptr->next != NULL)  
            {  
                ptr1 = ptr;  
                ptr = ptr ->next;  
            }  
            ptr1->next = NULL;  
            free(ptr);  
            printf("\nDeleted Node from the last ...\n");  
        }     
    }  
    void random_delete()  
    {  
        struct node *ptr,*ptr1;  
        int loc,i;    
        printf("\n Enter the location of the node after which you want to perform deletion \n");  
        scanf("%d",&loc);  
        ptr=head;  
        for(i=0;i<loc;i++)  
        {  
            ptr1 = ptr;       
            ptr = ptr->next;  
                  
            if(ptr == NULL)  
            {  
                printf("\nCan't delete");  
                return;  
            }  
        }  
        ptr1 ->next = ptr ->next;  
        free(ptr);  
        printf("\nDeleted node %d ",loc+1);  
    }  
    
      
    void display()  
    {  
        struct node *ptr;  
        ptr = head;   
        if(ptr == NULL)  
        {  
            printf("Nothing to print");  
        }  
        else  
        {  
            printf("\nprinting values . . . . .\n");   
            while (ptr!=NULL)  
            {  
                printf("\n%d",ptr->data);  
                ptr = ptr -> next;  
            }  
        }  
    }     

       
 
Output
 


 
          

No comments:

Post a Comment