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
Now we create the basic environment of the link list.
#include<stdio.h> #include<conio.h> struct slinearlinklist { int i; struct slinearlinklist *next; }; typedef struct slinearlinklist node; //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 *); node *head=NULL; 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"); printf("Enter Your choice : "); scanf("%d",&c); switch(c) { case 8: head=delete_first(head); // Delete From beginning break; case 9: delete_end(head); // Delete From end break; case 10: printf("Enter position : ");//Delete From a said position except first and last scanf("%d",&pos); delete_pos1(head,pos); break; case 11: printf("Enter position : ");//Delete From anywhare scanf("%d",&pos); delete_pos2(head,pos); break; case 12: head=delete_after(head); // Delete after a value break; case 13: head=delete_before(head); // Delete before a value break; case 8: exit(0); } } }
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 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; } }
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; } }
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; } }
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); } } else//not found { printf("Supplied data not found"); } return h; } }
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); } } else//not found { printf("Supplied data not found"); } return h; } }