Wednesday 9 October 2013

Double link list

Double link list is also just modified form of one way link list. In one way list we can travel in one direction only but in double link list we can travel in both direction front & back. To make it possible each  node point to next node & also node just before that node.
Back of first node & front of last node is initialise To NULL.
double link list
                                              C code :-


 #include<stdio.h>  
 #include<stdlib.h>  
 #include<malloc.h>  
 struct node      // defining node data type   
 {  
      struct node *back;  
      int data;  
      struct node *fro;  
 }*head;      // global variable of type node  
 typedef struct node node;  
 main()  
 {  
      head=NULL;  
      int i,m,n,op;  
      printf("\n 1.create list");  
      printf("\n 2.insert at given position");  
      printf("\n 3.delete a node with given information\n");  
      while(1)  
      {  
      printf("\n enter ur choice :");  
      scanf("%d",&op);  
      switch(op)  
      {  
           case 1:  
                printf("\n no of element u want in list :");  
                scanf("%d",&n);  
                for(i=0;i<n;i++)  
                {  
                 printf("\n input data :");  
                 scanf("%d",&m);  
                 create(m);  
             }  
             display();  
             break;  
        case 2:  
             printf("\n inter position where u want to insert :");  
             scanf("%d",&n);  
             printf("\n insert data :");  
             scanf("%d",&m);  
             atpos(m,n);  
        //     display();  
             break;  
        case 3:  
             printf("\n insert data which u want to delete :");  
             scanf("%d",&m);  
             del(m);  
             display();  
             break;  
        default:  
             printf("\n\t\t exit ");  
             exit(0);  
             break;  
      }  
   }  
  }  
  int create(int num)  
  {  
       node *temp,*r;  
       temp=head;  
       r=(node*)malloc(sizeof(node)); // creating new node  
       r->data=num;     // assigning volue  
       if(head == NULL) // insert as first node  
       {  
            head=r;  
            r->fro=NULL;  
            r->back=NULL;  
       }  
       else  
       {  
            while(temp->fro!=NULL ) //move to last element  
            {  
                 temp=temp->fro;  
            }  
            r->fro=NULL; // front link of last node must be NULL;  
            temp->fro= r;  
            r->back=temp;  
       }  
  }  
  int atpos(int num,int pos)  
  {  
       node *temp,*r;  
       int p;  
       temp=head;  
       for(p=0;p<pos-1;p++) // move to that pos  
       {  
            temp=temp->fro;  
            if(temp == NULL)  
            {  
                 printf("\n this posion is not avoilable");  
                 return;  
            }  
       }  
       r=(node*)malloc(sizeof(node));  
       r->data=num;  
    temp->fro->back=r;  
       r->fro=temp->fro;  
       r->back=temp;  
       temp->fro=r;  
       display();  
  }  
  del(int num)  
  {  
       node *temp,*r;  
       temp=head;  
       if(temp->data==num)  //also search that node  
       {  
            head=temp->fro;  
            head->back==NULL;  
            free(temp);  
            return;  
       }  
       else  
       {  
       while(temp->fro->fro!=NULL) // link of node next to temp !=NULL  
       {  
            if(temp->fro->data == num) //data in next node of temp  
            {  
                 r=temp->fro;  
                 r->fro->back=temp;  
                 temp->fro=r->fro;  
                 free(r);  
                 return;  
            }  
       }  
  }  
  }  
  display()  
  {  
       node *temp;  
       temp=head;  
       printf("\n data elements :");  
       while(temp!=NULL)  
       {  
        printf(" %d ",temp->data);  
        temp=temp->fro;       
       }  
  }  

OUTPUT :-


No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets