C program to create a linked list with insert front, insert rear, delete front,delete rear, display functions;

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *link;
}*first=NULL;
void insert_front();
void insert_rear();
void delet_front();
void delet_rear();
void disp();
int main()
{
    int choice;
    while(1)
    {
        printf("\nEnter your choice\n");
        printf("1 to insert front\n");
        printf("2 to insert rear\n");
        printf("3 to delete front\n");
        printf("4 to delete rear\n");
        printf("5 to display linked list\n");
        printf("0 to exit\n");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:insert_front();
                    break;
            case 2:insert_rear();
                    break;
            case 3:delet_front();
                    break;
            case 4:delet_rear();
                    break;
            case 5:disp();
                    break;
            case 0: return 0;
                    break;
            default:printf("Invalid Entry\n");
        }
    }
}
void insert_front()
{
    struct node *new_node;
    new_node=(struct node *)malloc(sizeof(struct node) );
    printf("Enter an element\n");
    scanf("%d",&new_node->data);
    new_node->link=NULL;
    if(first==NULL)
    {
        first=new_node;
    }
    else
    {
        new_node->link=first;
        first=new_node;
    }
}
void insert_rear()
{
    struct node *new_node,*temp;
    new_node=(struct node *)malloc(sizeof(struct node) );
    printf("Enter an element\n");
    scanf("%d",&new_node->data);
    new_node->link=NULL;
    if(first==NULL)
    {
        first=new_node;
    }
    else
    {
        temp=first;
        while(temp->link!=NULL)
        {
            temp=temp->link;
        }
        temp->link=new_node;
    }
}
void delet_front()
{
    struct node *x;
    if(first==NULL)
        printf("Delete not possible, linked list empty!\n");
    else if(first->link==NULL)
    {
        printf("Element deleted is %d\n",first->data);
        free(first);
        first=NULL;
    }
    else
    {
        printf("Element deleted is %d\n",first->data);
        x=first;
        first=first->link;
        free(x);

        x=NULL;

    }
}
void delet_rear()
{
    struct node *temp;
    if(first==NULL)
        printf("Delete not possible, Linked list empty!\n");
    else if(first->link==NULL)
    {
        printf("Element deleted is %d\n",first->data);
        free(first);
        first=NULL;
    }
    else
    {
        temp=first;
        while(temp->link->link!=NULL)
        {
            temp=temp->link;
        }
        printf("Element deleted is %d\n",temp->link->data);
        free(temp->link);
        temp->link=NULL;
    }
}
void disp()
{
    struct node *temp;
    if(first==NULL)
    {
        printf("Display not possible,linked list empty!\n");
    }
    else if(first->link==NULL)
    {
        printf("\nDisplaying elements in linked list\n");
        printf("%d ",first->data);
    }
    else
    {
        temp=first;
        printf("\nDisplaying elements in linked list\n");
        while(temp!=NULL)
        {
            printf("%d\t",temp->data);
            temp=temp->link;
        }
        printf("\n");
    }
}


OUTPUT:


Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
3
Delete not possible, linked list empty!

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
4
Delete not possible, Linked list empty!

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
5
Display not possible,linked list empty!

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
1
Enter an element
10

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
5

Displaying elements in linked list
10
Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
1
Enter an element
20

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
1
Enter an element
30

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
5

Displaying elements in linked list
30      20      10

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
2
Enter an element
40

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
2
Enter an element
50

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
5

Displaying elements in linked list
30      20      10      40      50

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
3
Element deleted is 30

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
5

Displaying elements in linked list
20      10      40      50

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
3
Element deleted is 20

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
5

Displaying elements in linked list
10      40      50

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
4
Element deleted is 50

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
5

Displaying elements in linked list
10      40

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
4
Element deleted is 40

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
4
Element deleted is 10

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
4
Delete not possible, Linked list empty!

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
3
Delete not possible, linked list empty!

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
5
Display not possible,linked list empty!

Enter your choice
1 to insert front
2 to insert rear
3 to delete front
4 to delete rear
5 to display linked list
0 to exit
0

Popular Posts