Data Structure Circular Doubly Linked List Implementation

Circular Doubly linked list Implementation code is given below:

//code for Implementation of Circular Doubly linked list
#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *ll,*rl;
}*head=NULL;
struct node *tail;
int count()
{
    int length = 0;
    struct node *t;
    t=NULL;
    while(t != head)
    {
      if(t==NULL)
      { 
        t=head;
       }else{
        length++;
        t = t ->rl;
      }
    }
    return length;
}
void insertbeg(int item)
{
    struct node* newnode;
    newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = item;
    newnode->ll=newnode->rl=newnode;
    if(head == NULL)
    {
        head = tail = newnode;
    } 
    else{
        newnode->rl=head;
        head->ll=newnode;
        tail->rl=newnode;
        newnode->ll=tail;
        head = newnode;
    }
}
void insertend(int item)
{
    struct node* newnode;
    newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = item;
    newnode->ll=newnode->rl=newnode;
    if(head == NULL)
    {
        head = tail = newnode;
    }
    else{
        newnode->ll=tail;
        tail->rl=newnode;
        newnode->rl=head;
        head->ll=newnode;
        tail=newnode; 
    }
}
void insertpos(int item,int pos)
{
    struct node* newnode,*t;
    newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = item;
    newnode->ll=newnode->rl=newnode;
    if(pos > count()+1)
    {
       printf("\nInvalid position\n");
    }
    else if(pos == 1)
    {
         insertbeg(item);
    }
    else if(pos == count()+1)
    {
         insertend(item);
    }
    else{
        t = head;
        for(int i =1;i<pos-1;i++)
        {
            t=t->rl;
        }
        newnode->ll= t;
        newnode->rl=t->rl;
        t->rl->ll=newnode;
        t->rl=newnode;
        
    }
}
void deletebeg()
{
    struct node *t;
    t=head;
    if(head == NULL)
    {
        printf("\n List is empty");
    }
    else if(head->rl==head)
    {
        printf("Deleted data is %d \n",head->data);
        head = tail= NULL;
    }
    else{
        head = head->rl;
        head->ll=tail;
        tail->rl=head;
        printf("Deleted data is %d \n",t->data);
        free(t);
    }
}
void deleteend()
{
    struct node*t;
    t=tail;
    if(head == NULL)
    {
        printf("\n List is empty");
    }
    else if(head->rl==head)
    {
        printf("Deleted data is %d \n",head->data);
        head = tail= NULL;
    }
    else{
        tail= tail->ll;
        tail->rl=head;
        head->ll=tail;
        printf("Deleted data is %d \n",t->data);
        free(t);
    }
}
void deletepos(int pos)
{
    struct node *t1,*t2;
    t2=head;
    if(pos > count())
    {
       printf("\nInvalid position");
    }
    else if(pos == 1)
    {
        deletebeg();
    }
    else if(pos == count())
    {
        deleteend();
    }
    else{
        
        for(int i=1;i<pos;i++)
        {
            t1=t2;
            t2=t2->rl;
        }
        t1->rl=t2->rl;
        t2->rl->ll=t1;
        printf("Deleted data is %d \n",t2->data);
        free(t2);
    }
}
void display()
{
    struct node* temp;
    temp=head;
    if(head == NULL)
    {
        printf("List is Empty");
    }
    else{
        while(temp->rl != head)
        {
            printf("_%d_",temp->data);
            temp = temp ->rl;
        }
        printf("_%d_",temp->data);
    }
}
void main()
{
    int choice ,c,data,x;
    do{
    printf("press 1 to insert in begning\n");
    printf("press 2 to insert in end\n");
    printf("press 3 to insert in any positions\n");
    printf("press 4 to delete first element\n");
    printf("press 5 to delete end element\n");
    printf("press 6 to to delete in any position\n");
    printf("press 7 to display\n");
    printf("press 8 for the length of current list\n");
     printf("press 0 to Exit\n");
    fflush(stdin);
    scanf(" %d",&choice);
    switch(choice)
    {
        case 1 :
        printf(" Enter the data to insert :");
        scanf(" %d",&data);
        insertbeg(data);

        break;
        case 2 :
         printf(" Enter the data to insert :");
        scanf(" %d",&data);
        insertend(data);

        break;
        case 3 :
         printf(" Enter the data to insert :");
        scanf(" %d",&data);
         printf(" Enter the position to insert :");
        scanf(" %d",&x);
        insertpos(data,x);

        break;
        case 4 :
        deletebeg();
        break;
        case 5 :
        deleteend();
        break;
        case 6 :
         printf(" Enter the position to delete :");
        scanf(" %d",&x);
        deletepos(x);
        break;
        case 7 :
        display();
        break;
        case 8 :
        printf("\n the length of current list: %d \n",count());
        break;
        case 0: 
        exit(1);
        break;
        default:
        printf("\nEnter the correct value");

    }
    printf("\nEnter 1 to continue :");
    fflush(stdin);
    scanf(" %d",&c);
    }while(c==1);
}

Output

press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
1
 Enter the data to insert :11

Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
1
 Enter the data to insert :22

Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_22__11_
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
2
 Enter the data to insert :44

Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
2
 Enter the data to insert :55

Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_22__11__44__55_
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
3
 Enter the data to insert :99
 Enter the position to insert :2

Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_22__99__11__44__55_
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
4
Deleted data is 22

Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_99__11__44__55_
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
5
Deleted data is 55

Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_99__11__44_
Enter 1 to continue :1 
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
6
 Enter the position to delete :2
Deleted data is 11

Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_99__44_
Enter 1 to continue :1
press 1 to insert in begning
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
0

Leave a Reply

Your email address will not be published. Required fields are marked *

Solverwp- WordPress Theme and Plugin