Follow This Blog For more... 😊

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

👨‍💻Doubly Linked list All insertion and Deletion operations.

⚙️Here, we implemented the following Operations for DOUBLY LinkedList.

  1. Insert from Front.
  2. Insert from End.
  3. Insert at Position.
  4. Delete from Front.
  5. Delete from End.
  6. Delete at Position.
  7. Delete Particular Value.
  8. Display (to display created Singly linked list)
  9. Display ODD positions. 
  10. Display EVEN position.
  11. Find the position of a particular key.
  12. Find the length of doubly LinkedList.
  13. Clear list.

CODE:


    // Doubly Linked list All insertion and Deletion operations
    #include <stdio.h>
    #include <stdlib.h>

    struct node
    {
            int data;
        struct node *prev, *next;
    };
    struct node *first, *last;
    int count = 0;

    void insert_from_front();
    void insert_from_end();
    void insert_at_position();
    void delete_from_front();
    void delete_from_end();
    void delete_at_position();
    void delete_particular_value();
    void display_doubly_linkedlist();
    void display_odd_positions();
    void display_even_positions();
    void position_of_particular_key();
    void length_of_Doubly_linkedlist();
    void clear_list();
    int OPERATIONS();


    main()
    {
            int choice;
        do
        {
                choice = OPERATIONS();
            switch (choice)
            {
                case 0:
                printf("\n # Thank you %c\n-->Visit: https://LSkyeducation.blogspot.com\n\n", 2);
                exit(0);
            case 1:
                insert_from_front();
                break;
            case 2:
                insert_from_end();
                break;
            case 3:
                insert_at_position();
                break;
            case 4:
                delete_from_front();
                break;
            case 5:
                delete_from_end();
                break;
            case 6:
                delete_at_position();
                break;
            case 7:
                delete_particular_value();
                break;
            case 8:
                display_doubly_linkedlist();
                break;
            case 9:
                display_odd_positions();
                break;
            case 10:
                display_even_positions();
                break;
            case 11:
                position_of_particular_key();
                break;
            case 12:
                length_of_Doubly_linkedlist();
                break;
            case 13:
                clear_list();
                break;
            default:
                printf("please, Enter between 0 to 13 !!");
            }
        } while (choice != 0);
    }

    void insert_from_front()
    {
            struct node *newnode;
        newnode = (struct node *)malloc(sizeof(struct node));
        if (newnode == NULL)
        {
                printf("\n#Unable to Allocate memory !!");
        }
        else
        {
                if (first == NULL)
            {
                    first = newnode;
                newnode->next = NULL;
                newnode->prev = NULL;
                last = first;
            }
            else
            {
                    newnode->next = first;
                newnode->prev = NULL;
                first->prev = newnode;
                first = newnode;
            }
            printf("=>Enter data:");
            scanf("%d", &newnode->data);
            count++;
        }
    }

    void insert_from_end()
    {
            struct node *newnode;
        newnode = (struct node *)malloc(sizeof(struct node));
        if (newnode == NULL)
        {
                printf("\n#Unable to Allocate memory !!");
        }
        else
        {
                if (first == NULL)
            {
                    first = newnode;
                newnode->next = NULL;
                newnode->prev = NULL;
                last = first;
            }
            else
            {
                    newnode->prev = last;
                newnode->next = NULL;
                last->next = newnode;
                last = newnode;
            }
            printf("=>Enter data:");
            scanf("%d", &newnode->data);
            count++;
        }
    }

    void insert_at_position()
    {
            int i = 1, position;
        printf("=>Enter position between 1 to %d:", count + 1);
        scanf("%d", &position);
        if (position >= 1 && position <= count + 1)
        {
                if (position == 1)
            {
                    insert_from_front();
            }
            else if (position == count + 1)
            {
                    insert_from_end();
            }
            else
            {
                    struct node *newnode, *temp;
                newnode = (struct node *)malloc(sizeof(struct node));
                if (newnode == NULL)
                {
                        printf("\n#Unable to Allocate memory !!");
                }
                else
                {
                        if (first == NULL)
                    {
                            first = newnode;
                        newnode->next = NULL;
                        newnode->prev = NULL;
                        last = first;
                    }
                    else
                    {
                            temp = first;
                        while (i <= position - 2)
                        {
                                temp = temp->next;
                            i++;
                        }
                        newnode->next = temp->next;
                        newnode->prev = temp;
                        temp->next->prev = newnode;
                        temp->next = newnode;
                    }
                    printf("=>Enter data:");
                    scanf("%d", &newnode->data);
                    count++;
                }
            }
        }
        else
        {
                printf("\nPlease Enter between 1 to %d", count + 1);
        }
    }

    void delete_from_front()
    {
            struct node *temp;
        if (first == NULL)
        {
                printf("\n# List is EMPTY !!\n");
        }
        else
        {
                temp = first;
            if (temp->next == NULL)
            {
                    free(temp);
                first = NULL;
                last = NULL;
            }
            else
            {
                    first = temp->next;
                first->prev = NULL;
                free(temp);
            }
            printf("=>data is deleted Successfully.\n");
            count--;
        }
    }

    void delete_from_end()
    {
            struct node *temp;
        if (first == NULL)
        {
                printf("\n# List is EMPTY !!\n");
        }
        else
        {
                temp = last;
            if (temp == first)
            {
                    free(temp);
                first = NULL;
                last = NULL;
            }
            else
            {
                    temp->prev->next = NULL;
                last = temp->prev;
                free(temp);
            }
            printf("=>data is deleted Successfully.\n");
            count--;
        }
    }

    void delete_at_position()
    {
            int i = 1, position;
        if (first == NULL)
        {
                printf("\n# List is EMPTY !!\n");
        }
        else
        {
                printf("=>Enter Position between 1 to %d :", count);
            scanf("%d", &position);
            if (position >= 1 && position <= count)
            {
                    if (position == 1)
                {
                        delete_from_front();
                }
                else if (position == count)
                {
                        delete_from_end();
                }
                else
                {
                        struct node *temp1, *temp2;
                    temp1 = first;
                    while (i <= position - 2)
                    {
                            temp1 = temp1->next;
                        i++;
                    }
                    temp2 = temp1->next;
                    temp1->next->next->prev = temp1;
                    temp1->next = temp1->next->next;
                    free(temp2);
                    printf("=>data is deleted Successfully.\n");
                    count--;
                }
            }
            else
            {
                    printf("\n=>Invalid Position !!!  [enter between 1 to %d]\n", count);
            }
        }
    }

    void delete_particular_value()
    {
            int deleting_data;
        struct node *temp1, *temp2;
        temp1 = first;
        if (temp1 == NULL)
        {
                printf("\nList is Empty.\n");
        }
        else
        {
                printf("=>Enter Deleting data:");
            scanf("%d", &deleting_data);
            if (temp1->data == deleting_data)
            {
                    delete_from_front();
            }
            else
            {
                    while (temp1->next->data != deleting_data)
                {
                        if (temp1->next->next == NULL)
                    {
                            break;
                    }
                    temp1 = temp1->next;
                }
                if (temp1->next->next == NULL)
                {
                        if (temp1->next->data == deleting_data)
                    {
                            delete_from_end();
                    }
                    else
                    {
                            printf("\n=>deleting data %d is NOT FOUND !!\n", deleting_data);
                    }
                }
                else
                {
                        temp2 = temp1->next;
                    temp1->next->next->prev = temp1;
                    temp1->next = temp1->next->next;
                    free(temp2);
                    printf("\n=>data is deleted Successfully.\n");
                    count--;
                }
            }
        }
    }

    void display_doubly_linkedlist()
    {
            int i;
        struct node *temp;
        if (first == NULL)
        {
                printf("\n#List is EMPTY !!\n");
        }
        else
        {
                printf("\n=>Doubly linked list.\n");
            printf("\n| NODE No. | Previous node | Data of curent node | Next node |\n");
            for (i = 1, temp = first; temp != NULL; temp = temp->next, i++)
            {
                    if (temp == first)
                    printf("   NODE:%d\t|%d|      \t<---|%d|--->\t   |%d| \n", i, temp->prev, temp->data, temp->next);
                else
                    printf("   NODE:%d\t|%d|\t<---|%d|--->\t   |%d| \n", i, temp->prev, temp->data, temp->next);
            }
        }
    }

    int OPERATIONS()
    {
            int choice;
        printf("________________________________________________________________________\n");
        printf("->Operations of Doubly Linked list.");
        printf("\n |0|Exit");
        printf("\n |1|Insert from front\t| 7|delete particular value\t|13|Clear list");
        printf("\n |2|Insert from end\t| 8|Display Doubly linkedlist");
        printf("\n |3|Insert at position\t| 9|Display ODD positions ONLY");
        printf("\n |4|Delete from front\t|10|Display EVEN positions ONLY");
        printf("\n |5|Delete from end\t|11|Position of particular data");
        printf("\n |6|Delete at position\t|12|Length of Doubly linkedlist");
        printf("\nEnter your choice:");
        scanf("%d", &choice);
        return choice;
    }

    void display_odd_positions()
    {
            int i;
        struct node *temp;
        if (first == NULL)
        {
                printf("\n#List is EMPTY !!\n");
        }
        else
        {
                printf("\n=>Doubly linked list.(ONLY ODD POSITION NODES)\n");
            printf("\n| NODE No. | Previous node | Data of curent node | Next node |\n");
            for (i = 1, temp = first; temp != NULL; temp = temp->next, i++)
            {
                    if (i % 2 != 0)
                {
                        if (temp == first)
                        printf("   NODE:%d\t|%d|      \t<---|%d|--->\t   |%d| \n", i, temp->prev, temp->data, temp->next);
                    else
                        printf("   NODE:%d\t|%d|\t<---|%d|--->\t   |%d| \n", i, temp->prev, temp->data, temp->next);
                    //  printf("NODE:%d  previous node:|%d|<--data:|%d|\t-->next node:|%d|\n",i,temp->prev,temp->data,temp->next);
                }
            }
        }
    }

Comments

Popular Posts