×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
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
675    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
softetechnologies

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

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);
			}			
		}
		else//not found
		{
			printf("Supplied data not found");
		}
		return h;
	}
}
softetechnologies

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);
			}			
		}
		else//not found
		{
			printf("Supplied data not found");
		}
		return h;
	}
}
Create and insert node in a linear link list using C Display The Link List Elements In C
softetechnologies
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.
Sri Sri Ramakrishna Paramahamsa
One cannot be spiritual as long as one has shame, hatred, or fear.
Sri Sri Ramakrishna Paramahamsa
1832
32.39
Today So Far
Total View (Lakh)
softetechnologies
01/01/2018     33195
28/06/2017     29001
02/08/2017     26439
26/05/2018     25916
15/05/2017     22730
06/07/2017     21157
01/08/2017     20858
21/04/2018     18365
25/06/2018     17075
14/07/2017     15980
softetechnologies