FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
2.4k views
in Manipal University Jaipur B.Tech(CSE-III Sem) Data Structure Lab by Goeduhub's Expert (7.6k points)
Deletion after specified node in singly linked list

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Deletion after specified node in singly linked list

Algorithm to Delete specified node

STEP 1: IF HEAD = NULL

WRITE UNDERFLOW
    GOTO STEP 10
   END OF IF

STEP 2: SET TEMP = HEAD

STEP 3: SET I = 0

STEP 4: REPEAT STEP 5 TO 8 UNTIL I

STEP 5: TEMP1 = TEMP

STEP 6: TEMP = TEMP → NEXT

STEP 7: IF TEMP = NULL

WRITE "DESIRED NODE NOT PRESENT"
    GOTO STEP 12
    END OF IF

STEP 8: I = I+1

[END OF LOOP]

STEP 9: TEMP1 → NEXT = TEMP → NEXT

STEP 10: FREE TEMP

STEP 11: EXIT 

Example

 image output

Program

#include <stdio.h>

#include <stdlib.h>

void create(int);  

void delete();  

struct node  

{  

    int data;  

    struct node *next;  

};  

struct node *head;  

void main ()  

{  

    int choice,item;  

    do   

    {  

        printf("\n1.insert node\n2.Delete node\n3.Exit\n4.display\n5.Enter your choice: ");  

        scanf("%d",&choice);  

        switch(choice)  

        {  

            case 1:  

            printf("\nEnter the item\n");  

            scanf("%d",&item);  

            create(item);  

            break;   

            case 2:  

            delete();  

            break;   

            case 3:  

            exit(0);  

            break;

            case 4:

            display();

            break;

            default:  

            printf("\nPlease enter valid choice\n");  

        }                    

    }while(choice != 3);  

}  

void create(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;  

        int loc,i;   

        scanf("%d",&loc);  

        ptr=head;  

        for(i=0;i<loc;i++)  

        {  

            ptr1 = ptr;       

            ptr = ptr->next;  

              

            if(ptr == NULL)  

            {  

                printf("\nThere are less than %d elements in the list\n",loc);  

                return;  

            }  

        }  

        ptr1 ->next = ptr ->next;  

        free(ptr);  

        printf("\nDeleted %d node ",loc);  

    } 

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

 image output


For more Manipal University Jaipur B.Tech CSE-III Sem Data Structure Lab Experiments Click here


Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...