MASIGNASUKAv102
6510051498749449419

Linked List Data-Structure Program in C Language - Learn With AVRK

Loading...

Performing all operations in linked list data structure using C Language.

#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node
{
    int data;
    struct node *next;
};
struct node *head;
void beginsert();
void lastinsert();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
int main()
{
    int choice=0;
    printf("\n-------------Main menu-----------------");
    printf("\n\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from Beginning\n");
    printf("5.Delete from last\n6.Delete node at specified location\n7.Show all Elements\n8.Exit\n");
    while(choice!= 8)
    {
        printf("\n----------------------------------------");
        printf("\nEnter your choice From the main menu: ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            beginsert();
            break;
            case 2:
            lastinsert();
            break;
            case 3:
            randominsert();
            break;
            case 4:
            begin_delete();
            break;
            case 5:
            last_delete();
            break;
            case 6:
            random_delete();
            break;
            case 7:
            display();
            break;
            case 8:
            exit(0);
            break;
            default:
            printf("Ooops...!!\nSomething went wrong...");    
        }
    }
}
void beginsert()
{
    struct node *ptr;
    int item;
    ptr = (struct node*) malloc(sizeof(struct node*));
    if(ptr == NULL)
    {
        printf("OVERFLOW...");
    }
    else
    {
        printf("Enter value to be pushed : ");
        scanf("%d",&item);
        ptr->data = item;
        ptr->next = head;
        head = ptr;
        printf("Node inserted...");
    }
}
void lastinsert()
{
    struct node *ptr,*temp;
    int item;
    ptr = (struct node*)malloc(sizeof(struct node*));
    if(ptr == NULL)
    {
        printf("OVERFLOW...");
    }
    else
    {
        printf("Enter value to be pushed : ");
        scanf("%d",&item);
        ptr->data = item;
        if(head == NULL)
        {
            ptr -> next = NULL;
            head = ptr;
            printf("Node inserted...");
        }
        else
        {
            temp = head;
            while (temp -> next!= NULL)
            {
                temp = temp -> next;
            }
            temp->next = ptr;
            ptr->next = NULL;
            printf("Node inserted...");
        }
    }
}
void randominsert()
{
    int i,loc,item;
    struct node *ptr, *temp;
    ptr = (struct node *) malloc (sizeof(struct node*));
    if(ptr == NULL)
    {
        printf("OVERFLOW...");
    }
    else
    {
        printf("Enter value to be pushed : ");
        scanf("%d",&item);
        ptr->data = item;
        printf("Enter the location after which you want to insert : ");
        scanf("%d",&loc);
        temp=head;
        for(i=0;i<loc;i++)
        {
            temp = temp->next;
            if(temp == NULL)
            {
                printf("Can't insert...");
                return;
            }
        }
        ptr ->next = temp ->next;
        temp ->next = ptr;
        printf("Node inserted...");
    }
}
void begin_delete()
{
    struct node *ptr;
    if(head == NULL)
    {
        printf("Empty List...");
    }
    else
    {
        ptr = head;
        head = ptr->next;
        free(ptr);
        printf("Node deleted at the begining...");
    }
}
void last_delete()
{
    struct node *ptr,*ptr1;
    if(head == NULL)
    {
        printf("Empty List...");
    }
    else if(head -> next == NULL)
    {
        head = NULL;
        free(head);
        printf("Node deleted at last...");
    }
    else
    {
        ptr = head;
        while(ptr->next != NULL)
        {
            ptr1 = ptr;
            ptr = ptr ->next;
        }
        ptr1->next = NULL;
        free(ptr);
        printf("Node deleted at last...");
    }
}
void random_delete()
{
    struct node *ptr,*ptr1;
    int loc,i;
    printf("Enter the location of the node which you want to delete : ");
    scanf("%d",&loc);
    ptr=head;
    for(i=0;i<loc;i++)
    {
        ptr1 = ptr;
        ptr = ptr->next;
        if(ptr == NULL)
        {
            printf("Can't delete...");
            return;
        }
    }
    ptr1 ->next = ptr ->next;
    free(ptr);
    printf("Deleted node %d ",loc+1);
}
void display()
{
    struct node *ptr;
    ptr = head;
    if(ptr == NULL)
    {
        printf("Nothing to print...");
    }
    else
    {
        printf("Printing values....");
        while (ptr!=NULL)
        {
            printf("\n%d",ptr->data);
            ptr = ptr -> next;
        }
    }
}

You mast run the above code using Dev C++ compiler to get the exact output given below -

Output -

-------------Main menu-----------------

1.Insert in beginning
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node at specified location
7.Show all Elements
8.Exit

---------------------------------------

Enter your choice From the main menu : 1
Enter value to be pushed : 11
Node inserted...
---------------------------------------

Enter your choice From the main menu : 2
Enter value to be pushed : 22
Node inserted...
---------------------------------------

Enter your choice From the main menu : 3
Enter value to be pushed : 33
Enter the location after which you want to insert : 1
Node inserted...
---------------------------------------

Enter your choice From the main menu : 7
Printing values....
11
22
33
---------------------------------------

Enter your choice From the main menu : 4
Node deleted at the beginning...
---------------------------------------

Enter your choice From the main menu : 5
Node deleted at last...
---------------------------------------

Enter your choice From the main menu : 7
Printing values....
22
---------------------------------------

Enter your choice From the main menu : 8

--------------------------------
Process exited after 83.38 seconds with return value 0
Press any key to continue . .

Learn With AVRK

Learn With AVRK

Related Post