Untitled

 avatar
unknown
plain_text
2 months ago
3.2 kB
3
Indexable
#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data1;
    struct node *next;
};
struct node *head = NULL;
void createlist()
{
    struct node *n1, *n2, *n3, *n4, *n5, *n6, *n7;
    n1 = (struct node *)malloc(sizeof(struct node));
    n2 = (struct node *)malloc(sizeof(struct node));
    n3 = (struct node *)malloc(sizeof(struct node));
    n4 = (struct node *)malloc(sizeof(struct node));
    n5 = (struct node *)malloc(sizeof(struct node));
    n6 = (struct node *)malloc(sizeof(struct node));
    n7 = (struct node *)malloc(sizeof(struct node));

    n1->data1 = 10;
    n2->data1 = 20;
    n3->data1 = 30;
    n4->data1 = 40;
    n5->data1 = 50;
    n6->data1 = 60;
    n7->data1 = 70;
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = n5;
    n5->next = n6;
    n6->next = n7;
    n7->next = n1;

    head = n1;
}
void display()
{
    if (head == NULL)
    {
        printf("List is empty.\n");
        return;
    }
    struct node *temp = head;
    do
    {
        printf("%d ", temp->data1);
        temp = temp->next;
    } while (temp != head);
    printf("\n");
}
void delete_at_head()
{
    if (head == NULL)
       {
           printf("There is no node\n");
           return;
       }

    struct node *temp = head;
    struct node *last = head;
    while (last->next != head)
    {
        last = last->next;
    }
    //If there is only one node...
    if (head->next == head)
    {
        free(head);
        head = NULL;
        return;
    }
    head = head->next;
    last->next = head;
    free(temp);
}
void delete_at_tail()
{
    if (head == NULL)
    {
        printf("There is no node\n");
        return;
    }
    struct node *temp = head;
    struct node *prev = NULL;
    while (temp->next != head)
    {
        prev = temp;
        temp = temp->next;
    }
    //if there is only one node
    if (head->next == head)
    {
        free(head);
        head = NULL;
        return;
    }

    prev->next = head;
    free(temp);
}
void delete_at_middle()
{
    if (head == NULL)
        return;

    int position;
    printf("Enter position to delete: ");
    scanf("%d", &position);

    if (position == 1)
    {
        delete_at_head();
        return;
    }
    struct node *temp = head;
    struct node *prev = NULL;
    int count = 1;
    do
    {
        if (count == position)
        {
            prev->next = temp->next;
            free(temp);
            return;
        }
        prev = temp;
        temp = temp->next;
        count++;
    } while (temp != head);

    printf("Invalid position!\n");
}
int main()
{
    createlist();
    printf("Your Linked List is given below:\n");
    display();

    delete_at_head();
    printf("After deleting at head, your linked list is:\n");
    display();

    delete_at_tail();
    printf("After deleting at tail, your linked list is:\n");
    display();

    delete_at_middle();
    printf("After deleting at middle, your linked list is:\n");
    display();

    return 0;
}
Editor is loading...
Leave a Comment