FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
Create and insert node in a linear link list using C Display The Link List Elements In C
Delete node of a linear link list using C - Link list - C Language
1129    Arnab De    25/08/2019

Delete a node in a linear link list. We can delete a node from different position of the list depend on position or value. The type of deleting node are

1. Delete from beginning.
2. Delete from end
3. Delete from a said position except first and last
4. Delete from any position
5. Delete a node after a value
6. Delete a node before a value

Now we create the basic environment of the link list.

```#include<stdio.h>
#include<conio.h>
{
int i;
};

//prototype declaration

node *delete_first(node *);
void delete_end(node *);
void delete_pos1(node *,int);
node *delete_pos2(node *);
node *delete_after(node *);
node *delete_before(node *);

int main()
{
int c,i,pos;
while(1)
{
printf(" 8. Delete From begining\n");
printf(" 9. Delete From end position\n");
printf("10. Delete from a said position except first and last\n");
printf("11. Delete From position [Any where]\n");
printf("12. Delete a node after a said value\n");
printf("13. Delete a node before a said value\n");
printf("25. Exit\n");
scanf("%d",&c);
switch(c)
{

case 8:
break;
case 9:
break;
case 10:
printf("Enter position : ");//Delete From a said position except first and last
scanf("%d",&pos);
break;
case 11:
printf("Enter position : ");//Delete From anywhare
scanf("%d",&pos);
break;
case 12:
break;
case 13:
break;
case 8:
exit(0);
}
}
}
```

### Delete a node from begining

Delete a node from the beginning of the exsiting list. As it change the first node, it return a node pointer.

```node *delete_first(node *l)
{
node *temp;
if(l!=NULL)
{
temp=l;
free(l);
l=temp->next;
}
return l;
}
```

### Delete a node from end

Delete a node from the beginning of the exsiting list. As it change the first node, it return a node pointer.

```void delete_end(node *l)
{
if(l!=NULL)
{
while(l-<next->next!=NULL)
{
l=l->next;
}
free(l->next);
l->next=NULL;
}
}
```

### Delete from a said position except first and last

```void delete_pos1(node *l,int pos)
{
node *temp;
int i;
if(l!=NULL)
{
for(i=1;i<=pos-2;i++)
{
l=l->next;
}
temp=l->next->next;
free(l->next);
l->next=temp;
}
}
```

### Delete from a said position [Any Position]

```node *delete_pos2(node *l)
{
node *temp;
int pos,i,c;
node *h=l;
printf("Enter position : ");
scanf("%d",&pos);
c=count(l);
if(pos==1)
{
//first delete
h=del_first(l);
return h;
}
else if(c==pos)
{
//end delete
del_end(l);
return h;
}
else if(c<pos)
{
printf("Your position is larger than list");
}
else
{
//position delete
del_pos(l,pos);
return h;

}
}
```

### Delete a node after a said value

```node *delete_after(node *l)
{
node *temp,*h;
int val;
int fl=0,pos=0;
h=l;
if(l!=NULL)
{
pos=1;
printf("Enter a value(after we delete a node)");
scanf("%d",&val);
while(l->next!=NULL)
{
if(l->i==val)
{
fl=1;
break;
}
pos++;
l=l->next;
}
if(fl)//found
{
if(l->next==NULL)//last node   delete_pos2
{
printf("data found at Last Node");
}
else
{
delete_pos2(h,++pos);
}
}
{
}
return h;
}
}
```

### Delete a node before a said value

```node *delete_before(node *l)
{
node *temp,*h;
int val;
int fl=0,pos=0;
h=l;
if(l!=NULL)
{
pos=1;
printf("Enter a value(after we delete a node)");
scanf("%d",&val);
while(l->next!=NULL)
{
if(l->next->i==val)
{
fl=1;
break;
}
pos++;
l=l->next;
}
if(fl)//found
{
if(l->next==NULL)//last node   delete_pos2
{
printf("data found at Last Node");
}
else
{
delete_pos2(h,pos);
}
}
{
}
return h;
}
}
```
Create and insert node in a linear link list using C Display The Link List Elements In C
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
If you want to go east, don't go west.
Sri Sri Ramakrishna Paramahamsa
118
51.37
Today So Far
Total View (Lakh)
26/05/2018     41662
01/01/2018     35880
28/06/2017     33903
25/06/2018     33092
02/08/2017     32257
06/07/2017     26687
01/08/2017     26673
15/05/2017     26297
14/07/2017     21663
21/04/2018     20589