Follow This Blog For more... 😊

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

 ðŸ‘¨‍💻Circular Linked list All insertion and Deletion operations.

⚙️Here, we implemented the following Operations for CIRCULAR 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 Circular linked list)
CODE:

    // implement ALL insertion & deletion operations in circular linked list.
    #include <stdio.h>
    #include <stdlib.h>

    struct node
    {
            int data;
        struct node *link;
    };
    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_circular_linkedlist();
    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_circular_linkedlist();
                break;
            default:
                printf("please, Enter between 0 to 8 !!");
            }
        } while (choice != 0);
    }

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

    void insert_from_end()
    {
            struct node *newnode, *temp;
        newnode = (struct node *)malloc(sizeof(struct node));
        if (newnode == NULL)
        {
                printf("\nUnable to allocate memory !!\n");
        }
        else
        {
                if (first == NULL)
            {
                    first = newnode;
                last = first;
                last->link = first;
            }
            else
            {
                    temp = first;
                while (temp->link != first)
                {
                        temp = temp->link;
                }
                temp->link = newnode;
                last = newnode;
                last->link = first;
            }
            printf("=>Enter data:");
            scanf("%d", &newnode->data);
            count++;
        }
    }

    void insert_at_position()
    {
            int position;
        printf("=>Enter the 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
            {
                    int i;
                struct node *newnode, *temp;
                newnode = (struct node *)malloc(sizeof(struct node));
                temp = first;
                for (i = 1; i <= position - 2; i++)
                {
                        temp = temp->link;
                }
                newnode->link = temp->link;
                temp->link = newnode;
                printf("=>Enter data:");
                scanf("%d", &newnode->data);
                count++;
            }
        }
        else
        {
                printf("Please Enter between 1 to %d", count + 1);
        }
    }

    void delete_from_front()
    {
            struct node *temp;
        if (first == NULL)
        {
                printf("\nList is Empty.\n");
        }
        else
        {
                temp = first;
            if (temp->link == first)
            {
                    free(temp);
                first = NULL;
                last = NULL;
            }
            else
            {
                    first = temp->link;
                last->link = first;
                free(temp);
            }
            printf("=>data is deleted Successfully.\n");
            count--;
        }
    }

    void delete_from_end()
    {
            struct node *temp1, *temp2;
        if (first == NULL)
        {
                printf("\nList is Empty.\n");
        }
        else
        {
                temp1 = first;
            if (temp1->link == first)
            {
                    free(temp1);
                first = NULL;
                last = NULL;
            }
            else
            {
                    while (temp1->link->link != first)
                {
                        temp1 = temp1->link;
                }
                printf("%d\n", temp1);
                temp2 = temp1->link;
                last = temp1;
                last->link = first;
                free(temp2);
            }
            printf("=>data is deleted Successfully.\n");
            count--;
        }
    }

    void delete_at_position()
    {
            struct node *temp1, *temp2;
        int i = 1, position;
        if (first == NULL)
        {
                printf("\nList is Empty.\n");
        }
        else
        {
                printf("\n=>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
                {
                        temp1 = first;
                    while (i <= position - 2)
                    {
                            temp1 = temp1->link;
                        i++;
                    }
                    temp2 = temp1->link;
                    temp1->link = temp1->link->link;
                    free(temp2);
                    printf("=>data is deleted Successfully.\n");
                    count--;
                }
            }
            else
            {
                    printf("Please Enter between 1 to %d", count);
            }
        }
    }

    void delete_particular_value()
    {
            struct node *temp1, *temp2;
        int deleting_data;
        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->link->data != deleting_data)
                {
                        temp1 = temp1->link;
                    if (temp1->link == first)
                    {
                            break;
                    }
                }
                if (temp1->link == first)
                {
                        printf("=>deleting data %d is NOT FOUND !!\n", deleting_data);
                }
                else
                {
                        temp2 = temp1->link;
                    temp1->link = temp1->link->link;
                    free(temp2);
                    printf("=>data is deleted Successfully.\n");
                    count--;
                }
            }
        }
    }

    void display_circular_linkedlist()
    {
            struct node *temp;
        int i = 1;
        if (first == NULL)
        {
                printf("\nList is Empty !!!\n");
        }
        else
        {
            printf("\n->Circular Linked list.\n");
            temp = first;
            printf("\n=>Node:%d\tdata:|%d|\t-->link:|%d|", i, temp->data, temp->link);
            i++;
            for (temp = temp->link; temp != first; temp = temp->link, i++)
            {
                    printf("\n=>Node:%d\tdata:|%d|\t-->link:|%d|", i, temp->data, temp->link);
            }
            printf("\n");
        }
    }

    int OPERATIONS()
    {
            int choice;
        printf("\n___________________________________________________________________");
        printf("\n->Operations of Circular Linked list.");
        printf("\n0.Exit");
        printf("\n1.Insert from front");
        printf("\n2.Insert from end");
        printf("\n3.Insert at position");
        printf("\n4.Delete from front");
        printf("\n5.Delete from end");
        printf("\n6.Delete at position");
        printf("\n7.delete particular value");
        printf("\n8.Display circular LinkedList");
        printf("\nEnter your choice:");
        scanf("%d", &choice);
        return choice;
    }

Comments

Popular Posts