Follow This Blog For more... 😊

Singly linked list ALL insertion & deletion operations.| Data Structure |Using C language|

👨‍💻 implement ALL insertion & deletion operations in the Singly linked list.

⚙️Here, implemented the following Operations.

  1. Insert from Front.
  2. Insert from End.
  3. Insert at Position.
  4. Delete from Front.
  5. Delete from End.
  6. Delete at Position.
  7. Delete Particular Value.
  8. Display (to display created Singly linked list)

CODE:


    //implement ALL insertion & deletion operations in singly linked list
    #include <stdio.h>
    #include <stdlib.h>
   
    struct node
    {
            int data;
        struct node *link;
    };
    struct node *first,*last;
    int count=0;

    void insert_from_front();
    void insert_from_end();
    void insert_at_position();
    void delete_from_front();
    void delete_from_end();
    void delete_at_position();
    void delete_particular_value();
    void display_singly_linkedlist();
    int OPERATIONS();


    main()
    {
            int choice;
        do
        {
                choice=OPERATIONS();
            switch(choice)
            {
                    case 0: printf("\n # Thank you %c\n-->Visit: https://LSkyeducation.blogspot.com\n\n",2);exit(0);
                case 1: if(count<=0){count=0;}insert_from_front();count++;break;
                case 2: if(count<=0){count=0;}insert_from_end();count++;break;
                case 3: if(count<=0){count=0;}insert_at_position();count++;break;
                case 4: delete_from_front();break;
                case 5: delete_from_end();break;
                case 6: delete_at_position();break;
                case 7: delete_particular_value();break;
                case 8: display_singly_linkedlist();break;
                default: printf("please, Enter between 0 to 8!!");
            }
        }while(choice!=0);
    }

    void insert_from_front()
    {
            struct node *newnode;
        newnode=(struct node *)malloc(sizeof(struct node));
        if(newnode==NULL)
        {
                printf("\nUnable to allocate memory !!\n");
        }
        else
        {
                if(first==NULL)
            {
                    first=newnode;
                newnode->link=NULL;
                last=first;
            }
            else
            {
                    newnode->link=first;
                first=newnode;
            }
            printf("=>Enter data:");
            scanf("%d",&newnode->data);
        }
    }

    void insert_from_end()
    {
            struct node *newnode,*temp;
        newnode=(struct node *)malloc(sizeof(struct node));
        if(newnode==NULL)
        {
                printf("\nUnable to allocate memory !!\n");
        }
        else
        {
                if(first==NULL)
            {
                    first=newnode;
                newnode->link=NULL;
                last=first;
            }
            else
            {
                    temp=first;
                while(temp->link!=NULL)
                {
                        temp=temp->link;
                }
                temp->link=newnode;
                newnode->link=NULL;
                last=newnode;
            }
            printf("=>Enter data:");
            scanf("%d",&newnode->data);
        }
    }

    void insert_at_position()
    {
            int position;
        printf("=>Enter the position between 1 to %d :",count+1);
        scanf("%d",&position);
        if(position>=1&&position<=count+1)
        {
                if(position==1)
            {
                    insert_from_front();
            }
            else if(position==count+1)
            {
                    insert_from_end();
            }
            else
            {
                    int i;
                struct node *newnode,*temp;
                newnode=(struct node *)malloc(sizeof(struct node));
                temp=first;
                for(i=1;i<=position-2;i++)
                {
                        temp=temp->link;
                }
                newnode->link=temp->link;
                temp->link=newnode;
                printf("=>Enter data:");
                scanf("%d",&newnode->data);
            }
        }
        else
        {
                printf("Please Enter between 1 to %d",count+1);
        }
    }

    void delete_from_front()
    {
            struct node *temp;
        if(first==NULL)
        {
                printf("\nList is Empty.\n");
        }
        else
        {
                temp=first;
            if(temp->link==NULL)
            {
                    free(temp);
                first=NULL;
                last=NULL;
            }
            else
            {
                    first=temp->link;
                free(temp);
            }
            printf("=>data is deleted Successfully.\n");
            count--;
        }
    }

    void delete_from_end()
    {
            struct node *temp;
        if(first==NULL)
        {
                printf("\nList is Empty.\n");
        }
        else
        {
                temp=first;
            if(temp->link==NULL)
            {
                    free(temp);
                first=NULL;
                last=NULL;
            }
            else
            {
                    while(temp->link->link!=NULL)
                {
                        temp=temp->link;
                }
                free(temp->link->link);
                temp->link=NULL;
                last=temp;
            }
            printf("=>data is deleted Successfully.\n");
            count--;
        }
    }

    void delete_at_position()
    {
            struct node *temp1,*temp2;
        int i=1,position;
        if(first==NULL)
        {
                printf("\nList is Empty.\n");
        }
        else
        {
                printf("\n=>Enter position between 1 to %d :",count);
            scanf("%d",&position);
            if(position>=1&&position<=count)
            {
                    if(position==1)
                {
                        delete_from_front();
                }
                else if(position!=count)
                {
                        temp1=first;
                    while(i<=position-2)
                    {
                            temp1=temp1->link;
                        i++;
                    }
                    temp2=temp1->link;
                    temp1->link=temp1->link->link;
                    free(temp2);
                    printf("=>data is deleted Successfully.\n");
                    count--;    
                }
                else
                {
                            delete_from_end();  
                }
            }
            else
            {
                    printf("Please Enter between 1 to %d",count);
            }
        }
    }

    void delete_particular_value()
    {
            struct node *temp1,*temp2;
        int deleting_data;
       
        temp1=first;
        if(temp1==NULL)
        {
                printf("\nList is Empty.\n");
        }
        else
        {
                printf("=>Enter deleting data:");
            scanf("%d",&deleting_data);
            if(temp1->data==deleting_data)
            {
                    delete_from_front();
            }
            else
            {      
                    while(temp1->link->data!=deleting_data)
                {
                        temp1=temp1->link;
                    if(temp1->link==NULL)
                    {
                            break;
                    }
                }
                if(temp1->link==NULL)
                {
                        printf("=>deleting data %d is NOT FOUND !!\n",deleting_data);
                }
                else
                {
                        temp2=temp1->link;
                    temp1->link=temp1->link->link;
                    free(temp2);
                    printf("=>data is deleted Successfully.\n");
                    count--;
                }
            }
        }
    }

    void display_singly_linkedlist()
    {
            struct node *temp;
        int i=1;
        if(first==NULL)
        {
                printf("\nList is Empty !!!\n");
        }
        else
        {
                printf("\n->Singly Linked list.\n");
            for(temp=first;temp!=NULL;temp=temp->link,i++)
            {
                    printf("\n=>Node:%d\tdata:|%d|\t-->link:|%d|",i,temp->data,temp->link);
            }
            printf("\n");
        }
    }

    int OPERATIONS()
    {
            int choice;
        printf("\n___________________________________________________________________\n");
        printf("\n->Operations of Singly Linked list.");
        printf("\n0.Exit");
        printf("\n1.Insert_from_front;");
        printf("\n2.Insert_from_end;");
        printf("\n3.Insert_at_position;");
        printf("\n4.Delete_from_front");
        printf("\n5.Delete_from_end");
        printf("\n6.Delete_at_position");
        printf("\n7.delete_particular_value");
        printf("\n8.Display_singly_linkedlist");
        printf("\nEnter your choice:");
        scanf("%d",&choice);
        return choice;
    }


OUTPUT:



Comments

Popular Posts