C program to search and replace key nodes in a linked list
PROGRAM:
#include <stdio.h>#include <stdlib.h>
//Creating structure
struct node
{
int data;
struct node *link;
}*first=NULL;
//function prototypes
struct node * create(struct node *);
int replace(struct node *,int,int);
void disp(struct node *);
//main function
int main()
{
int oldkey,newkey,count=0;
printf("\nEnter elements to create linked list:\n");
first=create(first);
printf("\nLinked list created!\n");
disp(first);
printf("\nEnter key to be replaced in the linked list\n");
scanf("%d",&oldkey);
printf("\nEnter new key\n");
scanf("%d",&newkey);
count=replace(first,oldkey,newkey);
if(count==0)
printf("\nKey not found\n");
else
printf("\nNo. of keys replaced= %d\n",count);
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 search and replace keynode in the linked list
int replace(struct node *first,int oldkey, int newkey)
{
struct node *temp;
int cnt=0;
if(first==NULL)
return 0;
else
{
temp=first;
while(temp!=NULL)
{
if(temp->data==oldkey)
{
printf("\nKey found!\n");
temp->data=newkey;
++cnt;
temp=temp->link;
}
else
temp=temp->link;
}
return cnt;
}
}
//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;
}
}
}
OUTPUT:
TRIAL 1:
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
10
Enter 1 to continue, 0 to stop
1
Enter an element
30
Enter 1 to continue, 0 to stop
1
Enter an element
10
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 10 30 10 20 10
Enter key to be replaced in the linked list
10
Enter new key
50
Key found!
Key found!
Key found!
Key found!
No. of keys replaced= 4
Displaying linked list elements:
50 40 50 30 50 20 50
TRIAL 2:
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 to be replaced in the linked list
70
Enter new key
80
Key not found
Displaying linked list elements:
60 50 40 30 20 10