Data Structure Doubly Linked list Implementation

Doubly linked list Implementation code is given below:

//code for Implementation of 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=head;
    while(t != NULL)
    {
        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=NULL;
    if(head == NULL)
    {
        head = tail = newnode;
    } 
    else{
        newnode->rl=head;
        head->ll = newnode;
        head = newnode;
    }
}
void insertend(int item)
{
    struct node* newnode;
    newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = item;
    newnode->ll=newnode->rl=NULL;
    if(head == NULL)
    {
        head = tail = newnode;
    }
    else{
        tail->rl=newnode;
        newnode->ll=tail;
        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=NULL;
    if(pos > count())
    {
       printf("\nInvalid position");
    }
    else if(pos == 1)
    {
         insertbeg(item);
    }
    else{
        t = head;
        for(int i =1;i<pos-1;i++)
        {
            t=t->rl;
        }
        newnode->ll= t;
        newnode->rl=t->rl;
        t->rl=newnode;
        t->rl->ll=newnode;
    }

}
void deletebeg()
{
    struct node *t;
    if(head == NULL)
    {
        printf("\n List is empty");
    }
    else if(head->rl==NULL)
    {
        printf("\n Deleted data is %d",head->data);
        tail = head = NULL;
    }
    else{
        t=head;
        head = head->rl;
        head->ll=NULL;
        printf("\n Deleted data is %d",t->data);
        free(t);
    }
}
void deleteend()
{
    struct node*t;
    if(head == NULL)
    {
        printf("\n List is empty");
    }
    else if(head->rl==NULL)
    {
        printf("\n Deleted data is %d",head->data);
        tail = head = NULL;
    }
    else{
        t=tail;
        tail= tail->ll;
        tail->rl=NULL;
        printf("\n Deleted data is %d",t->data);
        free(t);
    }
}
void deletepos(int pos)
{
    if(pos > count())
    {
       printf("\nInvalid position");
    }
    else if(pos == 1)
    {
        deletebeg();
    }
    else if(pos == count())
    {
        deleteend();
    }
    else{
        struct node *t,*p;
        t=head;
        for(int i=1;i<pos-1;i++)
        {
            t=t->rl;
        }
        p=t->rl;
        t->rl=p->rl;
        p->rl->ll=t;
        printf("\n Deleted data is %d",p->data);
        free(p);
    }
}
void display()
{
    struct node* temp;
    temp=head;
    if(head == NULL)
    {
        printf("List is Empty");
    }
    else{
        while(temp != NULL)
        {
            printf("_%d_",temp->data);
            temp = temp ->rl;
        }
    }
}
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",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 :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
_55__22__11__33__66_
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 element is 66
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
_55__22__11__33_
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 element 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
_55__11__33_
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
8

 the length of current list :3
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
0

Leave a Reply

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

Solverwp- WordPress Theme and Plugin