Complete Double Circular Linked List

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
8.2 kB
0
Indexable
Never
#include<stdio.h>
#include<stdlib.h>

typedef struct Node
{
    int data;
    struct Node *nxt, *prev;
}Node;

Node *creating_linked_list(int size)
{
    int i;
    Node *nodes[size],*head;
    for(i = 0; i < size; i++)
    {
        nodes[i] = (Node*)malloc(sizeof(Node));
        printf("Enter the data to be stored in the node %d: ",i+1);
        scanf("%d",&(nodes[i]->data));
        printf("\n");
    }
    head = nodes[0];
    for(i = 0; i < size; i++)
    {
        if(i == size-1)
        {
            nodes[i]->nxt = head;
            nodes[i]->prev = nodes[i-1];
        }
        else if(i == 0)
        {
            nodes[i]->prev = nodes[size-1];
            nodes[i]->nxt = nodes[i+1];
        }
        else
        {
            nodes[i]->prev = nodes[i-1];
            nodes[i]->nxt = nodes[i+1];
        }
    }
    return(head);
}

int display(Node **head)
{
    Node *temp = *head;
    if(*head == NULL)
    {
        printf("\nLinked List Doesn't Exist!!!\n");
        return(0);
    }
    printf("\n");
    do
    {
        printf("%d -> ", temp->data);
        temp = temp->nxt;
    }while(temp != *head);
    printf("The End!\n");
    return(1);
}

int search(Node **head, int key)
{
    if(*head == NULL)
    {
        printf("\nLinked List Doesn't Exist!!!\n");
        return(0);
    }
    else
    {
        Node *temp = *head;
        do
        {
            if(key == (temp->data))
            {
                return(1);
            }
            temp = temp->nxt;
        } while (temp != *head);
        return(0);
    }
}
int counting(Node **head)
{
    if(*head == NULL)
    {
        return(0);
    }
    else
    {
        int count = 1;
        Node *temp = *head;
        do
        {
            count++;
            temp = temp->nxt;
        } while (temp->nxt != *head);
        return(count);
    }
}
Node *insertion_at_beginning(Node **head, int new_data)
{
    Node *new_node = (Node*)malloc(sizeof(Node));
    new_node->data = new_data;
    if(*head == NULL)
    {
        printf("\nSince there are no nodes, so creating the linked list using the given data\n");
        *head = new_node;
        new_node->nxt = *head;
        new_node->prev = *head;
        return(*head);
    }
    else
    {
        Node *temp = *head;
        new_node->nxt = temp;
        while (temp->nxt != *head)
        {
            temp = temp->nxt;
        }
        new_node->prev = temp;
        temp->nxt = new_node;
        *head = new_node;
        return(*head);
    }
}
int insertion_at_specific_position(Node **head, int position, int new_data)
{
    if(*head == NULL)
    {
        printf("\nLinked List Doesn't Exist!!!\n");
        return(0);
    }
    else
    {
        Node *temp, *new_node = (Node*)malloc(sizeof(Node));
        new_node->data = new_data;
        temp = *head;
        for(int i = 0; i < position-1; i++)
        {
            temp = temp->nxt;
        }
        new_node->nxt = temp->nxt;
        temp->nxt = new_node;
        new_node->prev = temp;
        return(1);
    }
}
int insertion_at_ending(Node **head, int new_data)
{
    if(*head == NULL)
    {
        printf("\nLinked List Doesn't Exist!!!\n");
        return(0);
    }
    else
    {
        Node *temp = *head;
        Node *new_node = (Node *)malloc(sizeof(Node));
        new_node->data = new_data;
        new_node->nxt = *head;
        while (temp->nxt != *head)
        {
            temp = temp->nxt;
        }
        new_node->prev = temp;
        temp->nxt = new_node;
        return(1);
    }
}

Node *deletion(Node **head, int data)
{
    if(*head == NULL)
    {
        printf("\nLinked List Doesn't Exist!!!\n");
        return(NULL);
    }
    else
    {
        if(search(&(*head),data))
        {
            Node *temp = *head;
            if(temp->nxt == temp)
            {
                printf("\nOnly One Node Exists, And Now it is deleted\n");
                free(*head);
                *head = NULL;
                return(*head);
            }
            else if(temp->nxt != temp && temp->data == data)
            {
                *head = temp->nxt;
                temp->nxt->prev = temp->prev;
                Node *last_node = temp;
                while(last_node->nxt != temp)
                {
                    last_node = last_node->nxt;
                }
                last_node->nxt = temp->nxt;
                free(temp);
                return(*head);
            }
            else
            {
                Node *prev_node;
                do
                {
                    if(temp->data == data)
                    {
                        prev_node->nxt = temp->nxt;
                        temp->nxt->prev = prev_node;
                        free(temp);
                        return(*head);
                    }
                    prev_node = temp;
                    temp = temp->nxt;
                } while (temp != *head);
                
            }
        }
        else
        {
            printf("\n%d is not present in the linked list!!!!\n",data);
            return(NULL);
        }
    }
}

void deallocation(Node **head)
{
    int n = counting(&(*head));
    if(*head == NULL || n == 0)
    {
        exit(0);
    }
    else
    {/*
        still i have doubt in this !!! if u know pls help me out.....
    */
        Node *temp = *head;
        do
        {
            Node *current = temp;
            free(temp);
            current = current->nxt;
        } while (temp != *head);
        exit(0);
    }
}
void main()
{
    Node *head = NULL;
    int user_choice, size, data, position;
    printf("Welcome!!!");
    start:
    printf("\nSelect anyone of the following operations:\nCreating a Linked List --- 0\nDisplay the Linked List --- 1\nInsertion at Beginning --- 2\nInsertion at Specific Position --- 3\nInsertion at Ending --- 4\nDeletion --- 5\nSearching --- 6\nCount Number of Nodes --- 7\nExit --- 8\n");
    scanf("%d",&user_choice);
    switch (user_choice)
    {
    case 0:
        printf("\nEnter the required number of nodes: ");
        scanf("%d",&size);
        head = creating_linked_list(size);
        goto start;
        break;
    case 1:
        display(&head);
        goto start;
        break;
    case 2:
        printf("\nEnter the data to be inserted: ");
        scanf("%d",&data);
        head = insertion_at_beginning(&head,data);
        goto start;
        break;
    case 3:
        printf("\nEnter the position after which you want to perform insertion (indexing starts from 1 onwards): ");
        scanf("%d",&position);
        printf("\nEnter the data to be inserted at a specific position: ");
        scanf("%d",&data);
        insertion_at_specific_position(&head, position, data);
        goto start;
        break;    
    case 4:
        printf("\nEnter the data to be inserted at ending: ");
        scanf("%d",&data);
        insertion_at_ending(&head, data);
        goto start;
        break;    
    case 5:
        printf("\nEnter the data to be deleted: ");
        scanf("%d",&data);
        head = deletion(&head,data);
        goto start;
        break;    
    case 6:
        printf("\nEnter the data to be searched: ");
        scanf("%d",&data);
        if(search(&head,data))
        {
            printf("\nElement Found!!!\n");
        }
        else
        {
            printf("\nElement Not Found!!!\n");
        }
        goto start;
        break;
    case 7:
        data = counting(&head);
        if(data)
        {
            printf("\nNumber of Nodes: %d\n",data);
        }
        else
        {
            printf("\nLinked List doesn't exist!!!\n");
        }
        goto start;
        break;
    case 8:
        printf("Thank You ....");
        deallocation(&head);
        break;
    default:
        printf("\nPlease enter a valid option!!!\n");
        goto start;
        break;
    }
}