Follow This Blog For more... 😊

Circular Doubly linked list insertion & deletion operations.| Data Structure | Using C language|

👉implement insertion & deletion operations in the Circular Doubly Linked list.

⚙️Here, we implemented the following Operations.

  1. Insert from Front (First).
  2. Insert from End (Last).
  3. Insert at Position.
  4. Delete from Front (First).
  5. Delete from End (Last).
  6. Delete at Position.
  7. Display (to display created Circular Doubly linked list).
  8. Find the length of the Circular Doubly Linked List.
  9. Clear List (it is used to delete the whole list).

👨‍💻 Implement Operations of the Circular Doubly Linked list.

CODE:

    // Implement Operations of Circular Doubly Linked list
    #include <stdio.h>
    #include <stdlib.h>

    struct node
    {
        int data;
        struct node *llink, *rlink; // llink=Left node link , rlink= right node link
    };
    struct node *first = NULL, *last = NULL; // here first,last and count are globle variables
    int count = 0;

    void insert_first();
    void insert_last();
    void insert_at_position();
    void delete_first();
    void delete_last();
    void delete_at_position();
    void display();
    void length_of_Circular_Doubly_linkedlist();
    void clear_list();
    int operation_list();


    main()
    {
        int choice;
        do
        {
            choice = operation_list();
            switch (choice)
            {
            case 0:
                printf("\n # Thank you %c\n-->Visit: https://LSkyeducation.blogspot.com\n\n", 2);
                exit(0);
            case 1:
                insert_first();
                break;
            case 2:
                insert_last();
                break;
            case 3:
                insert_at_position();
                ;
                break;
            case 4:
                delete_first();
                break;
            case 5:
                delete_last();
                break;
            case 6:
                delete_at_position();
                break;
            case 7:
                display();
                break;
            case 8:
                length_of_Circular_Doubly_linkedlist();
                break;
            case 9:
                clear_list();
                break;
            default:
                printf("please, Enter between 0 to 7 !!");
            }
        } while (choice != 0);
    }

    // FUNCTION :1
    void insert_first()
    {
        struct node *newnode;
        newnode = (struct node *)malloc(sizeof(struct node));
        if (newnode == NULL)
        {
            printf("Unable to allocate memory, PLEASE try again !!!");
        }
        else
        {
            newnode->llink = NULL;
            newnode->rlink = NULL;
            if (first == NULL)
            {
                first = newnode;
                newnode->llink = newnode;
                newnode->rlink = newnode;
                last = newnode;
                printf("Enter data:");
                scanf("%d", &newnode->data);
            }
            else
            {
                first->llink = newnode;
                newnode->rlink = first;
                first = newnode;
                last->rlink = newnode;
                newnode->llink = last;
                printf("Enter data:");
                scanf("%d", &newnode->data);
            }
            count++;
            printf("->data:%d inserted successfully.", newnode->data);
        }
    }

    // FUNCTION :2
    void insert_last()
    {
        struct node *newnode;
        newnode = (struct node *)malloc(sizeof(struct node));
        if (newnode == NULL)
        {
            printf("Unable to allocate memory, PLEASE try again !!!");
        }
        else
        {
            newnode->llink = NULL;
            newnode->rlink = NULL;
            if (first == NULL)
            {
                first = newnode;
                newnode->llink = newnode;
                newnode->rlink = newnode;
                last = newnode;
                printf("Enter data:");
                scanf("%d", &newnode->data);
            }
            else
            {
                last->rlink = newnode;
                newnode->rlink = first;
                newnode->llink = last;
                last = newnode;
                first->llink = last;
                printf("Enter data:");
                scanf("%d", &newnode->data);
            }
            count++;
            printf("->data:%d inserted successfully.", newnode->data);
        }
    }

    // FUNCTION :3
    void insert_at_position()
    {
        struct node *newnode;
        struct node *temp;
        int position, i = 1;
        newnode = (struct node *)malloc(sizeof(struct node));
        if (newnode == NULL)
        {
            printf("Unable to allocate memory, PLEASE try again !!!");
        }
        else
        {
            newnode->llink = NULL;
            newnode->rlink = NULL;
            printf("Enter position in range 1 to %d : ", count + 1);
            scanf("%d", &position);
            if (position >= 1 && position <= (count + 1))
            {
                if (position == 1)
                {
                    insert_first();
                }
                else if (position == (count + 1))
                {
                    insert_last();
                }
                else
                {
                    temp = first;
                    while (i != position - 1)
                    {
                        temp = temp->rlink;
                        i++;
                    }
                    newnode->rlink = temp->rlink;
                    newnode->llink = temp->rlink->llink;
                    temp->rlink->llink = newnode;
                    temp->rlink = newnode;
                    printf("Enter data:");
                    scanf("%d", &newnode->data);
                    count++;
                    printf("->data:%d inserted successfully.", newnode->data);
                }
            }
        }
    }

    // FUNCTION :4
    void delete_first()
    {
        if (first == NULL)
        {
            printf("# List is Empty #");
        }
        else
        {
            if (first == last)
            {
                first = NULL;
                last = NULL;
            }
            else
            {
                struct node *temp;
                temp = first;
                last->rlink = temp->rlink;
                first = first->rlink;
                first->llink = last;
                free(temp);
            }
            count--;
            printf("->data deleted successfully.");
        }
    }

    // FUNCTION :5
    void delete_last()
    {
        if (first == NULL)
        {
            printf("# List is Empty #");
        }
        else
        {
            if (first == last)
            {
                first = NULL;
                last = NULL;
            }
            else
            {
                struct node *temp;
                temp = last;
                temp->llink->rlink = first;
                last = temp->llink;
                first->llink = last;
                free(temp);
            }
            count--;
            printf("->data deleted successfully.");
        }
    }

    // FUNCTION :6
    void delete_at_position()
    {
        int position, i = 1;
        struct node *temp;
        if (first == NULL)
        {
            printf("# List is Empty #");
        }
        else
        {
            printf("Enter position in range 1 to %d : ", count);
            scanf("%d", &position);
            if (position >= 1 && position <= (count))
            {
                if (position == 1)
                {
                    delete_first();
                }
                else if (position == (count))
                {
                    delete_last();
                }
                else
                {
                    temp = first;
                    while (i != position - 1)
                    {
                        temp = temp->rlink;
                        i++;
                    }
                    temp->rlink = temp->rlink->rlink;
                    temp->rlink->llink = temp;
                    count--;
                    printf("->data deleted successfully.");
                }
            }
        }
    }

    // FUNCTION :7
    void display()
    {
        struct node *temp;
        int i = 1;
        if (first == NULL)
        {
            printf("# List is Empty #");
        }
        else
        {
            printf("\n| NODE No. | Previous node | Data of curent node | Next node |\n");
            temp = first;
            printf("   NODE:%d\t|%d|\t<---|%d|--->\t   |%d| \n", i, temp->llink, temp->data, temp->rlink);
            i++;
            for (temp = temp->rlink; temp != first; temp = temp->rlink)
            {
                printf("   NODE:%d\t|%d|\t<---|%d|--->\t   |%d| \n", i, temp->llink, temp->data, temp->rlink);
                i++;
            }
        }
    }

    // FUNCTION :8
    int operation_list()
    {
        int choice;
        printf("\n___________________________________________________________________");
        printf("\n->Operations of Circular Doubly linked list.");
        printf("\n0.Exit");
        printf("\n1.Insert first");
        printf("\n2.Insert last");
        printf("\n3.Insert at position");
        printf("\n4.Delete first");
        printf("\n5.Delete last");
        printf("\n6.Delete at position");
        printf("\n7.Display List");
        printf("\n8.Length of Circular Doubly linked list");
        printf("\n9.Clear List");
        printf("\n=>Which Operation you want to perform:");
        scanf("%d", &choice);
        return choice;
    }

    // FUNCTION :9
    void length_of_Circular_Doubly_linkedlist()
    {
        printf("\n-->Length of the Doubly Linkedlist is :%d\n", count);
    }

    // FUNCTION :10
    void clear_list()
    {

        if (first == NULL)
        {
            printf("# List is Already Empty #");
            return;
        }
        while (count != 0)
        {
            if (first == last)
            {
                first = NULL;
                last = NULL;
            }
            else
            {
                struct node *temp;
                temp = last;
                temp->llink->rlink = first;
                last = temp->llink;
                first->llink = last;
                free(temp);
            }
            count--;
        }
        printf("->List Cleared successfully.");
    }

Comments

Popular Posts