#include <stdio.
h>
#include <stdlib.h>
// Structure for a node in a linked list
struct node {
int data;
struct node *next;
};
// Global pointers to track the head and tail of the list
struct node *head = NULL, *tail = NULL;
// Function to insert n nodes at the end of the list
void insertNodeList(int n) {
int num, i;
struct node *newnode;
for (i = 1; i <= n; i++) {
newnode = (struct node *)malloc(sizeof(struct node));
if (newnode == NULL) {
printf("Memory cannot be allocated.\n");
return;
printf("Input data for node %d: ", i);
scanf("%d", &num);
newnode->data = num;
newnode->next = NULL;
if (head == NULL) {
head = newnode;
tail = newnode;
} else {
tail->next = newnode;
tail = newnode;
// Function to display the linked list
void displayList() {
struct node *tmp;
if (head == NULL) {
printf("List is empty.\n");
} else {
tmp = head;
while (tmp != NULL) {
printf("Data = %d\n", tmp->data);
tmp = tmp->next;
// Function to search for a value and replace it
void search_replace(int n) {
int f = 0;
int d;
struct node *prev;
for (prev = head; prev != NULL; prev = prev->next) {
if (prev->data == n) {
f = 1;
printf("Enter data to replace: ");
scanf("%d", &d);
prev->data = d;
return;
if (f == 0)
printf("Element not found in the list\n");
}
int main() {
int n, searchVal;
printf("Enter number of nodes to insert: ");
scanf("%d", &n);
insertNodeList(n);
printf("\nLinked List before replacement:\n");
displayList();
printf("\nEnter value to search and replace: ");
scanf("%d", &searchVal);
search_replace(searchVal);
printf("\nLinked List after replacement:\n");
displayList();
return 0;