Friday, January 28, 2011

Double Linked List Program in C

 /*  
      dlinkedlist.c: This programs is implementation of doubly linked list.   
      @uthor: itsafiz@gmail.com   
      Date: 11Oct2010.   
 */  
 #include<stdio.h>   
 typedef struct   
      {  
           int data;   
           struct dnode *next;   
           struct dnode *prev;   
      }dnode;  
 typedef dnode *list;   
 list head, tail;   
 main()  
 {  
      list temp,paste;   
      head=NULL;   
      int ch, n;   
      while(1)  
      {  
      printf("\t##Double Linked list ##\n\t1.Add element\n\t2.Display\n\t3.Delete\n\t4.Exit\n");  
      scanf("%d",&ch);  
      switch(ch)  
           {  
           case 1:   
           temp=(list)malloc(sizeof(dnode));  
           printf("Enter Data = ");  
           scanf("%d",&n);  
           temp->data=n;  
           if(head==NULL)  
           {  
           temp->prev=NULL;   
           head=temp;   
           }  
           else  
           {  
           temp->prev=tail;  
           tail->next=temp;  
           }  
           temp->next=NULL;  
           tail=temp;   
           break;   
           case 2:   
                paste=head;  
                for(;paste!=NULL;paste=paste->next)  
                     {  
                          printf("%d\t",paste->data);  
                     }  
                printf("\n");  
           break;   
           case 3: break;   
           case 4: exit(1);   
           }  
      }  
 }