DS – Double Linked List – Insertion – Display

Create():

  • Creating list of elements.
  • “start” pointer always pointing to first node
  • “new” pointer points to newly created node
  • “last” pointer points to last node in the list.
  • We create nodes continuously using loops.

Append():

  • Add a node at the end of the list.
  • In list, we always maintain only one pointer(start) which is pointing to first node in the list.
  • To append(add a node at the end), we need to travel from first location to last location using only temporary pointer variable and loop.
  • Once we reach the last node in the list, we need to add the new node at the end.

Length():

  • It is used to find the length of list.
  • If no nodes in the list, it returns 0 else it returns node count.

Display():

  • Function is used to display the elements of list.
  • If list is empty, it returns with “Error message : Empty list”
  • If not empty, it displays all nodes data.

Add at Begin:

  • Create a node.
  • If the list is empty, it will be the start node
  • If not empty, we need to connect the new node at the beginning of list.

Addafter:

  • Adding the node after specified node.
  • If the location is not available – return with error message
  • If location is present, create the node and add.
void addafter(int data){
            struct Node *new, *p;
            int loc, len, i;
            printf(“Enter loc to add after : “);
            scanf(“%d”, &loc);
            len=length();
            if(loc>len){
                        printf(“Invalid location \n”);
            }
            else{
                        p=root;
                        i=1;     
                        while(i<loc){
                                    p=p->right;
                                    i++;
                        }
                        new = malloc();
                        new->data = data;
                        if(p->right){
                                    p->right->left=new;
                        }
                        new->right=p->right;
                        new->left=p;
                        p->right=new;
            }
}

Scroll to Top