Singly Linked List - Deletion Operation | Data Structure

 

In this post I will show you how we perform deletion operation in Singly Linked List in different ways. Like:

1. How we delete node at First ?

2. How we delete node at Last ?

3. How we delete node at specific position ?

I will give you a complete logic for every case.

Graphical Representation:


// Complete Program for delete node form  Link List

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>


struct node

{

    int data;

    struct node *next;

};


Copy Code

// Function for display Link List

struct node *display(struct node *start)

{

    struct node *temp;

    temp=start;

    while(temp!=NULL)

    {

        printf("->%d",temp->data);

        temp=temp->next;

    }

    return(start);

}



Copy Code

// Function for deleting node from beginning of Link List

struct node *delbegg(struct node *start)

{

    struct node *temp;

    if(start==NULL)

    {

        printf("Empty List !");

    }

    else

    {

        temp=start;

        start=start->next;

        free(temp);

    }

    return(start);

}


Copy Code

// Function for deleting node from last of Link List

struct node *dellast(struct node *start)

{

    struct node *temp,*prev;

    

    if(start==NULL)

    {

        printf("Empty List");

    }

    else

    {

        temp=start;

        while(temp->next!=NULL)

        {

            prev=temp;

            temp=temp->next;

        }

        

        if(temp==start)

        {

            start=NULL;

            free(temp);

        }

        else

        {

            prev->next=NULL;

            free(temp);

        }

    }

     return(start);

}


Copy Code

// Function for deleting node from specific position of Link List

struct node *delpos(struct node *start,int x)

{

    struct node *temp=start,*prev;

    while(temp!=NULL)

    {

        if(temp->data!=x)

        {

            prev=temp;

            temp=temp->next;

        }

        else

        {

            if(temp==start)

            {

                start=start->next;

                free(temp);

            }

            else

            {

                prev->next=temp->next;

                free(temp);

            }

            return(start);

        }

    }

    printf("Data not found !");

    return(start);

}


Copy Code

// Main Function

void main()

{

    struct node *newnode,*oldnode,*start=NULL;

    int ch=1;

    do

    {

        newnode=(struct node *)malloc(sizeof(struct node));

        newnode->next=NULL;

        

        printf("Enter data:");

        scanf("%d",&newnode->data);

        //newnode->data=data;

        

        if(start==NULL)

        {

            start=newnode;

        }

        else

        {

            oldnode->next=newnode;

        }

        oldnode=newnode;

        

        printf("Do you want to 1-continue  2- Exit:");

        scanf("%d",&ch);

        

    }while(ch!=2);


    printf("Link List is:");

    display(start);

    printf("\n");

    

    printf("What you want to do ?\n");

    printf("1-Display 2-Delete at First  3-Delete at Last  4-Delete at Specific Position :");

    scanf("%d",&ch);

    switch(ch)

    {

        case 1:

        {

           printf("Link List is:"); 

           display(start);

           break;

           

        }

        case 2:

        {

           printf("Link List after deleting node from begnning is:");

           display(delbegg(start)); 

           break;

        }

        case 3:

        {

            printf("Link List after deleting node from last is:");

            display(dellast(start));

            break;

        }

        case 4:

        {

          int p;

          printf("Enter Element you want to delete:");

          scanf("%d",&p);

          printf("Link List after deleting node from from %d position is:",p);

          display(delpos(start,p)); 

          break;

        }

        default :

        {

        printf("Invalid Choice");

        }

    }         

    getch();

}


Out Put:

Display Link List


Delete Node From Beginning of Link List


Delete Node From Last of Link List


Delete Node from specific position of Link List



No comments:

Post a Comment