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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 50
    Comment on it

    In doubly linked list we have two portion reserved for addresses one for the next address and other for the previous address.

     

    Doubly linked list hold the reference for both the list either previous to it or next to it and the middle portion contains the data for the node.

     

    Doubly Linked List

     

     

    Basic Operations

    • Insertion − add element at beginning of the list.

    • Deletion − delete an element from the beginning of the list.

    • Insert Last − add an element in the end of the list.

    • Delete Last − delete an element from the end of the list.

    • Insert After − add an element after an item of the list.

    • Delete − delete an element from the list .

    • Display forward − displaying complete list in going forward.

    • Display backward − displaying complete list in going backward.

     

    Insertion in Doubly Linked List

     

    //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()) {
          //make it the last link
          last = link;
       }else {
          //update first prev link
          head->prev = link;
       }
    
       //point it to old first link
       link->next = head;
    	
       //point first to new first link
       head = link;
    }

     

     

    Deletion in Doubly Linked List

    While deletion just free the previous position by giving its position to the previous node and the next position to another if it exists otherwise set NULL for both

     

    //delete first item
    struct node* deleteFirst() {
    
       //save reference to first link
       struct node *tempLink = head;
    	
       //if only one link
       if(head->next == NULL) {
          last = NULL;
       }else {
          head->next->prev = NULL;
       }
    	
       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: