0% found this document useful (0 votes)
2 views4 pages

Linked List Operations in C

This document contains a C program that implements a linked list with functionalities to insert nodes, display the list, and search for a value to replace it. It defines a structure for the linked list nodes and includes functions to manage the list. The main function orchestrates the user input for node insertion and value replacement, displaying the list before and after the replacement.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Linked List Operations in C

This document contains a C program that implements a linked list with functionalities to insert nodes, display the list, and search for a value to replace it. It defines a structure for the linked list nodes and includes functions to manage the list. The main function orchestrates the user input for node insertion and value replacement, displaying the list before and after the replacement.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#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;

You might also like