Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Circular Doubly Linked List

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 234
    Comment on it

    This is an implementation of Circular Doubly Linked List in C Programming Language.


     

    #include stdio.h 
    
    #include stdlib.h 
    
     struct node
    
         {
    
           int root, *next, *prev;
    
           struct node n; 
    
         } 
    
    
     n* create_node(int); 
    
     void add_node(); 
    
     void insert_at_beg(); 
    
     void insert_at_end(); 
    
     void insert_at_pos(); 
    
     void delete_node(); 
    
     void sort_list(); 
    
     void search(); 
    
     void display_from_beg(); 
    
     void display_from_end();
    
    
    n *new, *ptr, *prev;
    
    n *root = NULL, *last = NULL;
    
    int number = 0;
    
    void main()
    
    {
    
        int ch;
    
    
    printf("\n Linked List\n");
    
    printf("1. Insert at beginning \n 
    
        2. Insert at end \n 
    
        3. Insert at position \n
    
        4. Sort linked list \n 
    
        5. Delete node at position \n 
    
            6. Search element \n
    
            7. Display from beginning \n
    
            8. Display from end \n
    
        9. Add Node \n
    
            10. Exit ");
    
    
    while (1)
    
    {
    
    
        printf("\nEnter your choice:");
    
        scanf("%d", &ch);
    
        switch(ch)
    
        {
    
        case 1 :
    
            insert_at_beg();
    
            break;
    
        case 2 :
    
            insert_at_end();
    
            break;
    
        case 3 :
    
            insert_at_pos();
    
            break;
    
        case 4 :
    
            sort_list();
    
            break;
    
        case 5 :
    
            delete_node();
    
            break;
    
        case 6 :
    
            search();
    
            break;
    
        case 7 :
    
            display_from_beg();
    
            break;
    
        case 8 :
    
            display_from_end();
    
            break;
    
        case 10 :
    
            exit(0);
    
        case 9 :
    
            add_node();
    
            break;
    
        default:
    
            printf("\nInvalid Choice");   
                
    
        }
    
    }
    
    }
    
    n* create_node(int info)             //Creating
    new node
    
    {
    
        number++;
    
        new = (n *)malloc(sizeof(n));
    
        new->val = info;
    
        new->next = NULL;
    
        new->prev = NULL;
    
        return new;
    
    }
    
    void add_node()             //Adding new node
    
    {
    
    
    int info;
    
    
    printf("\nEnter the value:");
    
    scanf("%d", &info);
    
    new = create_node(info);
    
    
    if (root == last && root == NULL)
    
    {
    
    
        root = last = new;
    
        root->next = last->next = NULL;
    
        root->prev = last->prev = NULL;
    
    }
    
    else
    
    {
    
        last->next = new;
    
        new->prev = last;
    
        last = new;
    
        last->next = root;
    
        root->prev = last;
    
    }
    
    }
    
    void insert_at_beg()               //Element
    Insertion at beginning
    
    {
    
    
    int info;
    
    
    printf("\nEnter the value:");
    
    scanf("%d",&info);
    
    new = create_node(info);
    
    
    if (root == last && root == NULL)
    
    {    
    
        printf("\nLinked List is empty now");
    
        root = last = new;
    
        root->next = last->next = NULL;
    
        root->prev = last->prev = NULL;
    
    }
    
    else
    
    {
    
        new->next = root;
    
        root->prev = new;
    
        root = new;
    
        root->prev = last;
    
        last->next = root;
    
        printf("\n Value has been inserted at
    the begining");
    
    }
    
    }
    
    void insert_at_end()            //Element
    Insertion at end
    
    {
    
    
    int info;    
    
    
    printf("\nEnter the value:");
    
    scanf("%d", &info);
    
    new = create_node(info);
    
    
    if (root == last && root == NULL)
    
    {
    
        root = last = new;
    
        root->next = last->next = NULL;    
    
        root->prev = last->prev = NULL;
    
    }
    
    else
    
    {
    
        last->next = new;
    
        new->prev = last;
    
        last = new;
    
        root->prev = last;
    
        last->next = root;
    
    }
    
    }
    
    void insert_at_pos()                       
    //Element insertion at given position
    
    {
    
        int info, pos, len = 0, i;
    
        n *prevnode;
    
    
    printf("\nEnter the value:");
    
    scanf("%d", &info);
    
    printf("\nEnter the position:");
    
    scanf("%d", &pos);
    
    new = create_node(info);
    
    
    if (root == last && root == NULL)
    
    {
    
        if (pos == 1)
    
        {
    
            root = last = new;
    
            root->next = last->next = NULL;   
    
            root->prev = last->prev = NULL;
    
        }
    
        else
    
            printf("\nEmpty Linked List");
    
    }
    
    else
    
    {
    
        if (number < pos)
    
            printf("\nOverflow of elements");
    
    
        else
    
        {
    
            for (ptr = root, i = 1; i <= number;
    i++)
    
            {
    
                prevnode = ptr;
    
                ptr = ptr->next;
    
                if (i == pos-1)
    
                {
    
                    prevnode->next = new;
    
                    new->prev = prevnode;
    
                    new->next = ptr;
    
                    ptr->prev = new;
    
                    printf("\nInserted at
    position %d succesfully", pos);
    
                    break;
    
                }
    
            }
    
        }
    
    }
    
    }
    
    void sort_list()                   //Sorting
    
    { 
    
        n *temp;
    
        int tempval, i, j;
    
    
    if (first == last && first == NULL)
    
        printf("\nlinked list is empty no
    elements to sort");
    
    else
    
    {
    
        for (ptr = first,i = 0;i < number;ptr =
    ptr->next,i++)
    
        {
    
            for (temp = ptr->next,j=i;j<number;j++)
    
            {
    
                if (ptr->val > temp->val)
    
                {
    
                    tempval = ptr->val;
    
                    ptr->val = temp->val;
    
                    temp->val = tempval;
    
                }
    
            }
    
        }
    
        for (ptr = root, i = 0; i < number; ptr =
    ptr->next,i++)
    
            printf("\n%d", ptr->val);
    
    }
    
    }
    
    void delete_node()                       //Node
    Deletion
    
    { 
    
        int pos, count = 0, i;
    
        n *temp, *prevnode;
    
    
    printf("\nEnter the position of the
    element:");
    
    scanf("%d", &pos);
    
    
    if (root == last && root == NULL)
    
        printf("\n Empty linked list");
    
    
    else
    
    {
    
        if (number < pos)
    
            printf("\nNode can not be deleted
    at position as it is exceeding the linkedlist length");
    
    
        else
    
        {
    
            for (ptr = root,i = 1;i <=
    number;i++)
    
            {
    
                prevnode = ptr;
    
                ptr = ptr->next;
    
                if (pos == 1)
    
                {    
    
                    number--;
    
                    last->next = prevnode->next;
    
                    ptr->prev = prevnode->prev;
    
                    root = ptr;
    
                    printf("%d is deleted",
    prevnode->val);
    
                    free(prevnode);
    
                    break;        
    
                }
    
                else if (i == pos - 1)
    
                {    
    
                    number--;
    
                    prevnode->next = ptr->next;
    
                    ptr->next->prev =
    prevnode;
    
                    printf("%d is deleted",
    ptr->val);
    
                    free(ptr);
    
                    break;
    
                }
    
            }
    
        }
    
    }
    
    }
    
    void search()                    //Search
    
    {
    
        int count = 0, key, i, f = 0;
    
    
    printf("\nEnter the element to be
    searched:");
    
    scanf("%d", &key);
    
    
    if (root == last && root == NULL)
    
        printf("\nList is empty");
    
    else
    
    {
    
        for (ptr = root,i = 0; i < number;
    i++,ptr = ptr->next)
    
        {
    
            count++;
    
            if (ptr->val == key)
    
            {
    
                printf("\nElement found at
    position %d", count);
    
                f = 1;
    
            }    
    
        }
    
        if (f == 0)
    
            printf("\nElement is not in linked
    list");
    
    }
    
    }
    
    void display_from_beg()                     
    //Elements display from beginning
    
    {
    
        int i;
    
        if (root == last && root == NULL)
    
            printf("\nList is empty");
    
        else
    
        { 
    
            printf("\n%d Number of nodes",
    number);
    
            for (ptr = root, i = 0; i < number;
    i++,ptr = ptr->next)
    
                printf("\n %d", ptr->val);
    
        }
    
    }
    
    void display_from_end()                   
    //Elements display from end
    
    {
    
        int i;
    
        if (root == last && root == NULL)
    
            printf("\nList is empty");
    
        else
    
        {
    
            for (ptr = last, i = 0; i < number;
    i++,ptr = ptr->prev)
    
            {
    
                printf("\n%d", ptr->val);
    
            }
    
        }
    
    }


     

    The code has been successfully compiled and executed.

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: