C program to create circular linked list with insert front, insert rear, delete front, delete rear and display elements 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()
{
    //Creating a new node N and temporary variable temp.
    struct node *N,*temp;
    N=(struct node *)malloc(sizeof(struct node*));
    printf("\nEnter an element\n");
    scanf("%d",&N->data);
    N->link=NULL;
    if(first==NULL)
    {
        first=N;
        first->link=first;
    }
    else
    {
        temp=first;
        while(temp->link!=first)
            temp=temp->link;
        temp->link=N;
        N->link=first;
        first=N;
    }
}

void insert_rear()
{
    //Creating a new node N and temporary variable temp.
    struct node *N,*temp;
    N=(struct node *)malloc(sizeof(struct node*));
    printf("\nEnter an element\n");
    scanf("%d",&N->data);
    N->link=NULL;
    if(first==NULL)
    {
        first=N;
        N->link=first;
    }
    else
    {
        temp=first;
        while(temp->link!=first)
        {
            temp=temp->link;
        }
        temp->link=N;
        N->link=first;
    }
}

void delet_front()
{
    struct node *temp,*x;
    if(first==NULL)
        printf("\nDeletion not possible, Linked list empty!\n");
    else if(first->link==first)
    {
        printf("Element deleted is %d",first->data);
        free(first);
        first=NULL;
    }
    else
    {
        printf("\nElement deleted is %d\n",first->data);
        temp=first;
        while(temp->link!=first)
            temp=temp->link;
        x=first;
        first=first->link;
        temp->link=first;
        free(x);
        x=NULL;
    }
}

void delet_rear()
{
    struct node *temp;
    if(first==NULL)
        printf("\nDeletion not possible, Linked list empty!\n");
    else if(first->link==first)
    {
        printf("\nElement deleted is %d\n",first->data);
        free(first);
        first=NULL;
    }
    else
    {
        temp=first;
        while(temp->link->link!=first)
            temp=temp->link;
        printf("\nElement deleted is %d\n",temp->link->data);
        free(temp->link);
        temp->link=first;
    }
}

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


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
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
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
4

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
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
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 linked list elements:
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 linked list elements:
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 linked list elements:
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 linked list elements:
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 linked list elements:
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 linked list elements:
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
5

Displaying linked list elements:
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

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
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
4

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
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
0

Popular Posts