C program to delete key nodes in a linked list
PROGRAM:
#include <stdio.h>#include <stdlib.h>
//Creating structure
struct node
{
int data;
struct node *link;
}*first=NULL;
struct node * create(struct node *);
void disp(struct node *);
struct node * delet(struct node *,int);
//main function
int main()
{
int key;
printf("\nEnter elements to create linked list\n");
first=create(first);
printf("\nLinked list created!\n");
disp(first);
printf("\nEnter key node to be deleted\n");
scanf("%d",&key);
first=delet(first,key);
disp(first);
return 0;
}
//function to create linked list
struct node * create(struct node *first)
{
struct node *new_node;
int choice=0;
do
{
new_node=(struct node *)malloc(sizeof(struct node));
printf("\nEnter an element\n");
scanf("%d",&new_node->data);
new_node->link=NULL;
if(first==NULL)
first=new_node;
else
{
new_node->link=first;
first=new_node;
}
printf("\nEnter 1 to continue, 0 to stop\n");
scanf("%d",&choice);
}while(choice==1);
return first;
}
//function to display linked list
void disp(struct node *first)
{
struct node *temp;
if(first==NULL)
printf("\nDisplay not possible, Linked list empty\n");
else if(first->link==NULL)
{
printf("Displaying linked list elements:\n");
printf("%d",first->data);
}
else
{
printf("Displaying linked list elements:\n");
temp=first;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->link;
}
}
}
//function to reverse linked list
struct node * delet(struct node * first,int key)
{
int cnt=0;
struct node *temp=NULL,*prev=NULL;
if(first==NULL)
{
printf("\nKey not found\n");
return first;
}
else
{
if(first->data==key)
{
printf("\nKey found\n");
temp=first;
first=first->link;
free(temp);
++cnt;
}
prev=first;
temp=first->link;
while(temp!=NULL)
{
if(temp->data==key)
{
printf("\nKey found\n");
prev->link=temp->link;
free(temp);
temp=prev->link;
++cnt;
}
else
{
prev=temp;
temp=temp->link;
}
}
if(cnt==0)
printf("\nKey not found\n");
else
printf("%d keys deleted\n",cnt);
return first;
}
}
OUTPUT:
TRIAL 1:
Enter elements to create linked listEnter an element
10
Enter 1 to continue, 0 to stop
1
Enter an element
20
Enter 1 to continue, 0 to stop
1
Enter an element
10
Enter 1 to continue, 0 to stop
1
Enter an element
30
Enter 1 to continue, 0 to stop
1
Enter an element
40
Enter 1 to continue, 0 to stop
1
Enter an element
10
Enter 1 to continue, 0 to stop
0
Linked list created!
Displaying linked list elements:
10 40 30 10 20 10
Enter key node to be deleted
10
Key found
Key found
Key found
3 keys deleted
Displaying linked list elements:
40 30 20
TRIAL:
Enter elements to create linked list
Enter an element
10
Enter 1 to continue, 0 to stop
1
Enter an element
20
Enter 1 to continue, 0 to stop
1
Enter an element
30
Enter 1 to continue, 0 to stop
1
Enter an element
40
Enter 1 to continue, 0 to stop
1
Enter an element
50
Enter 1 to continue, 0 to stop
1
Enter an element
60
Enter 1 to continue, 0 to stop
0
Linked list created!
Displaying linked list elements:
60 50 40 30 20 10
Enter key node to be deleted
70
Key not found
Displaying linked list elements:
60 50 40 30 20 10
Enter an element
10
Enter 1 to continue, 0 to stop
1
Enter an element
20
Enter 1 to continue, 0 to stop
1
Enter an element
30
Enter 1 to continue, 0 to stop
1
Enter an element
40
Enter 1 to continue, 0 to stop
1
Enter an element
50
Enter 1 to continue, 0 to stop
1
Enter an element
60
Enter 1 to continue, 0 to stop
0
Linked list created!
Displaying linked list elements:
60 50 40 30 20 10
Enter key node to be deleted
70
Key not found
Displaying linked list elements:
60 50 40 30 20 10