0% found this document useful (0 votes)
20 views3 pages

Linked List

The document contains a C program that implements a singly linked list with functionalities to insert, delete, display, and search/replace elements. It defines a structure for nodes and provides functions for various operations, including inserting at the beginning or end, deleting from either end, and searching for a value to replace. The main function presents a menu for user interaction to perform these operations.

Uploaded by

cotoham700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Linked List

The document contains a C program that implements a singly linked list with functionalities to insert, delete, display, and search/replace elements. It defines a structure for nodes and provides functions for various operations, including inserting at the beginning or end, deleting from either end, and searching for a value to replace. The main function presents a menu for user interaction to perform these operations.

Uploaded by

cotoham700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;

int insertAtBegin()
{
int x;
struct node *p = (struct node*)malloc(sizeof(struct node));
printf("enter the value: ");
scanf("%d", &x);
p->data = x;
p->next = NULL;
if(head == NULL)
{
head = p;
current = p;
}
else
{
p->next = head;
head = p;
}
}

int insertAtEnd()
{
int x;
struct node *p = (struct node*)malloc(sizeof(struct node));
printf("enter the value: ");
scanf("%d", &x);
p->data = x;
p->next = NULL;
if(current == NULL)
{
head = p;
current = p;
}
else
{
current->next = p;
current = p;
}
}
int deleteFromBegin()
{
if(head == NULL)
{
printf("The list is empty");
}
else
{
head = head->next;
}
}

int deleteFromEnd()
{
if(head == NULL)
{
printf("The list is empty");
}
else if(head->next == NULL)
{
head = NULL;
current = NULL;
}
else
{
struct node *temp1, *temp2;
temp1 = head;
while(temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
temp2->next = NULL;
current = temp2;
}
}

int display()
{
struct node *temp = head;
while(temp != NULL) {
printf("%d \n", temp->data);
temp = temp->next;
}
}
int searchAndReplcae()
{
int num1, num2, count;
printf("enter number to search\n");
scanf("%d", &num1);
printf("enter number to replace\n");
scanf("%d", &num2);
struct node *temp = head;
while(temp != NULL)
{
count++;
if(temp->data == num1)
{
printf("Element is found at %d and replaced to %d", count, num2);
temp->data = num2;
break;
}
}
}

int main()
{
printf("1. Insert at beginning\n");
printf("2. Insert at the end\n");
printf("3. Delete from beginning\n");
printf("4. Delete from the end\n");
printf("5. Display the linked list elements\n");
printf("6. Search and Replace\n");
printf("7. Exit\n");
int choice;
while(1)
{
printf("Enter your choice");
scanf("%d", &choice);
switch(choice)
{
case 1: insertAtBegin();
break;
case 2: insertAtEnd();
break;
case 3: deleteFromBegin();
break;
case 4: deleteFromEnd();
break;
case 5: display();
break;
case 6: searchAndReplace();
break;
case 7: exit(1);
break;
}
}
}

You might also like