Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Circular Linked List in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 52
    Comment on it

    The circular linked list and the other  linked list whether single linked list or double linked list only have the difference that in doubly linked list the next position of the last node is NULL while in the circular linked list the last position of the next pointer is the address of the first node not NULL.

     

    Single Linked List as Circular

     

    Singly Linked List as Circular Linked List

     

     

    Doubly Linked List as Circular

     

    Doubly Linked List as Circular Linked List

     

     

    Basic Operation

    • insert − insert an element in the start of the list.

    • delete − insert an element from the start of the list.

    • display − display the list.

     

    Insertion in circular linked list

     

    While insertion you need to set the both node to the next and previous node and for the last node set the node next position to first node address

     

    //insert link at the first location
    void insertFirst(int key, int data) {
       //create a link
       struct node *link = (struct node*) malloc(sizeof(struct node));
       link->key = key;
       link->data= data;
    	
       if (isEmpty()) {
          head = link;
          head->next = head;
       }else {
          //point it to old first node
          link->next = head;
    		
          //point first to new first node
          head = link;
       }   
       
    }

     

    Deletion in circular linked list

     

    //delete first item
    struct node * deleteFirst() {
       //save reference to first link
       struct node *tempLink = head;
    	
       if(head->next == head){  
          head = NULL;
          return tempLink;
       }     
    
       //mark next to first link as first 
       head = head->next;
    	
       //return the deleted link
       return tempLink;
    }

     

     

    .net

 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: