# Deletion after specified node in singly linked list

0 like 0 dislike
2.4k views
Deletion after specified node in singly linked list

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

## 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

Program

#include <stdio.h>

#include <stdlib.h>

void create(int);

void delete();

struct node

{

int data;

struct node *next;

};

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:

}

}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;

printf("\nNode inserted\n");

}

}

void delete()

{

struct node *ptr, *ptr1;

int loc,i;

scanf("%d",&loc);

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;

if(ptr == NULL)

{

printf("Nothing to print");

}

else

{

printf("\nprinting values . . . . .\n");

while (ptr!=NULL)

{

printf("\n%d",ptr->data);

ptr = ptr -> next;

}

}

}

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.