Implementation of link list using C program

 

 Implementation of link list using C program

 


 #include <stdio.h>
#include <stdlib>

struct node
{
    int data;
    struct node *next;
} * head;

void begInsert();
void lastInsert();
void randomInsert();
void beginDelete();
void lastDelete();
void randomDelete();
void display();
void search();

int main()
{
    int choice;
    while (choice != 9)
    {
        printf("\n\t Main Menu\n");
        printf("\n Choose one option from the following list :\n");
        printf("\n 1. Insert in begenning\n 2. Insert at End \n 3. Insert at any point\n");
        printf(" 4. Delete begining data\n 5. Delete random data .\n 6. Delete last data\n");
        printf(" 7. Display All data\n 8. Search \n 9. Exit\n");
        printf("\n Enter your choice: ");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
            begInsert();
            break;
        case 2:
            lastInsert();
            break;
        case 3:
            randomInsert();
            break;
        case 4:
            beginDelete();
            break;
        case 5:
            randomDelete();
            break;
        case 6:
            lastDelete();
            break;
        case 7:
            display();
            break;
        case 8:
            search();
            break;
        case 9:
            exit(0);
        defaule:
            printf("\n INVALID INPUT \n");
        }
    }
    return 0;
}

void begInsert()
{
    struct node *ptr;
    int item;
    ptr = (struct node *)malloc(sizeof(struct node *));
    if (ptr == NULL)
    {
        printf("Overflow");
    }
    else
    {
        printf("Enter a value :");
        scanf("%d", &item);
        ptr->data = item;

        if (head == NULL)
        {
            ptr->next = NULL;
            head = ptr;
            printf("\n node inserted \n");
        }
        else
        {
            ptr->next = head;
            head = ptr;
            printf("\n node inserted \n");
        }
    }
}
void lastInsert()
{
    struct node *ptr, *temp;
    int item;
    ptr = (struct node *)malloc(sizeof(struct node*));
    
    if (ptr == NULL)
    {
        printf("\n OverFlow");
    }
    else
    {
        printf("Enter value:");
        scanf("%d", &item);
        ptr->data = item;
        ptr->next = NULL;

        if (head == NULL)
        {
            head = ptr;
            printf("Node Inserted");
        }
        else
        {
            temp = head;
            while (temp->next != NULL)
            {
                temp = temp->next;
            }
            temp->next = ptr;
            printf("node inserted");
        }
    }
}
void randomInsert()
{
    int i=1, loc, item;
    struct node *ptr, *temp;
    ptr = (struct node *)malloc(sizeof(struct node *));
    if (ptr == NULL)
    {
        printf("\n OverFlow");
    }
    else
    {
        
        printf("Enter location: ");
        scanf("%d", &loc);

        temp = head;
        if(loc==1){
            begInsert();
        }else{
            printf("Enter value\n");
            scanf("%d", &item);
            ptr->data = item;

            while (inext;
            if(temp==NULL){
                printf("Node is less than position");
                exit(0);
            }
            i=i+1;
        }
        ptr->next = temp->next;
        temp->next = ptr;

        printf("node inserted");
        }
        
    }
}
void beginDelete()
{
    struct node *ptr;
    if (head == NULL)
    {
        printf("\nList is empty");
    }
    else
    {
        ptr = head;
        head = ptr->next;
        free(ptr);
        printf("Node deleted from the beginning\n");
    }
}
void lastDelete()
{
    struct node *ptr, *ptr1;
    if (head == NULL)
    {
        printf("\nList is empty");
    }
    else if (head->next == NULL)
    {
        head = NULL;
        free(head);
        printf("Only node of list deleted \n");
    }
    else
    {
        ptr = head;
        while (ptr->next != NULL)
        {
            ptr1 = ptr;
            ptr = ptr->next;
        }
        ptr1->next = NULL;
        free(ptr);
        printf("Node deleted from the last\n");
    }
}
void randomDelete()
{
     struct node *temp=head,*temp1=head;
    int pos, count=1;
      printf("\n\tEnter location of data : ");
     scanf("%d", &pos);
    if (head==NULL)
    {
        printf("\n\tLink list is empty.\n");
        
    }else{
        while (temp1->next!=NULL)
        {
            temp1=temp1->next;
            count++;
        }
    
        if (pos==1)
        {
            beginDelete();
        }else if (pos==count)
        {
            lastDelete();
        }else{
          
            for (int i = 1; i < pos-1; i++)
            {
                temp=temp->next;
            
            }
            
            temp->next=temp->next->next;
            free(temp->next);
            printf("\n\tNode deleted of %d th location",pos);
            
        }
        
        
        
    }
}
void display()
{
    struct node *temp = head;

    while (temp != NULL)
    {
        printf("%d->", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}
void search()
{
    struct node *temp = head;
    int num, count = 1, flag=0;
    printf("Enter number to search:");
    scanf("%d", &num);
    if (temp->next == NULL)
    {
        printf("Stack is empty");
    }
    else
    {
        
        while (temp!=NULL)
        {
            if (num == temp->data)
            {
                printf("Data found at %d", count);
                flag=1;
                break;
            }
            
            temp = temp->next;
            count++;
        }
        if (flag==0)
        {
            printf("\n\tData not found");
            
        }
        
        
        
    }
} 
 
 

OUTPUT: