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