C program to create a doubly 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 *rlink;
    struct node *llink;
}*first=NULL;
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->llink=NULL;
    new_node->rlink=NULL;
    if(first==NULL)
    {
        first=new_node;
    }
    else
    {
        new_node->rlink=first;
        first->llink=new_node;
        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->rlink=NULL;
    new_node->llink=NULL;
    if(first==NULL)
    {
        first=new_node;
    }
    else
    {
        temp=first;
        while(temp->rlink!=NULL)
        {
            temp=temp->rlink;
        }
        temp->rlink=new_node;
        new_node->llink=temp;
    }
}
void delet_front()
{
    struct node *x;
    if(first==NULL)
        printf("Deletion not possible, linked list empty!\n");
    else if(first->rlink==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->rlink;
        free(x);
        x=NULL;
    }
}
void delet_rear()
{
    struct node *temp;
    if(first==NULL)
    {
        printf("Delete not possible, linked list empty!\n");
    }
    else if(first->rlink==NULL)
    {
        printf("Element deleted is %d",first->data);
        free(first);
        first=NULL;
    }
    else
    {
        temp=first;
        while(temp->rlink->rlink!=NULL)
        {
            temp=temp->rlink;
        }
        printf("Element deleted is %d",temp->rlink->data);
        free(temp->rlink);
        temp->rlink=NULL;
    }

}
void disp()
{
    struct node *temp;
    if(first==NULL)
    {
        printf("Display not possible, linked list empty!\n");
    }
    else if(first->rlink==NULL)
    {
        printf("Displaying data in the linked list:\n %d\n",first->data);
    }
    else
    {
        temp=first;
        printf("Displaying data in the linked list:\n");
        while(temp!=NULL)
        {
            printf("%d\t",temp->data);
            temp=temp->rlink;
        }
        printf("\n");

    }

}
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");
        printf("\nEnter your choice:");
        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");
        }
    }
    return 0;
}


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

Enter your choice:6
Invalid Entry!

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

Enter your choice: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

Enter your choice:3
Deletion 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

Enter your choice: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

Enter your choice: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

Enter your choice:5
Displaying data in the 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

Enter your choice: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

Enter your choice: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

Enter your choice:5
Displaying data in the 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

Enter your choice: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

Enter your choice: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

Enter your choice:5
Displaying data in the 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

Enter your choice: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

Enter your choice:5
Displaying data in the 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

Enter your choice: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

Enter your choice:5
Displaying data in the linked list:
20      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

Enter your choice: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

Enter your choice: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

Enter your choice:5
Displaying data in the 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

Enter your choice:3
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

Enter your choice: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

Enter your choice:3
Deletion 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

Enter your choice: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

Enter your choice:0



Popular Posts