Sunday, 25 August 2013

Doubly Linked List in C

Hi, This program is about implementation of Doubly linked list program in c.

If you have any doubt regarding this program or any concept of C then please send me at khimanichirag@gmail.com or post a comment.

suggestion always appreciable


      #include<stdio.h>  
      #include<conio.h>  
      void print();  
      void create();       
      void delet();  
      void search();  
      void show();  
      typedef struct node  
      {       
             int data;  
             struct node *link;  
             struct node *blink;  
      }node;  
      node *start=NULL,*top=NULL;  
      void lin(int n)  
      {  
             int i;  
             printf("n");  
             for(i=0;i<n;i++)  
             {  
               printf("*");  
             }  
             printf("n");  
      }  
      void main()  
      {  
             clrscr();  
             print();  
      }  
      void print()  
      {  
             clrscr();  
             printf("=== ALL THE INDEX START FROM 1 ==========");  
             lin(50);  
             printf("1.create a node");  
             printf("n2.delete a node");  
             printf("n3.search a node at specified index");  
             printf("n4.show the Link List");  
             lin(50);  
             printf("nChoose your option:");  
             flushall();  
             switch((getch()))  
             {  
               case '1':  
                    create();  
                    break;  
               case '2':  
                    delet();  
                    break;  
               case '3':  
                    search();  
                    break;  
               case '4':  
                    show();  
                    break;  
               default:  
                    printf("nInvalid choice!!!!n");  
             }  
             printf("nntYou want to do more operation.(Y/N):");  
             if(getch()=='y')  
           {  
                  print();  
           }  
      }  
      void create()  
      {  
              node *tptr;  
             int index,i=1;  
             char ch;  
             do  
             {  
               node *temp=(node*)malloc(sizeof(node));  
               clrscr();  
               lin(50);  
               printf("1.insert at bigginingn");  
               printf("n2.insert at the endn");  
               printf("n3.insert at specified indexn");  
               lin(50);  
               printf("nChoose you operation:n");  
               ch=getch();  
               if(ch=='1')  
               {  
                      if(start==NULL)  
                      {  
                        start=temp;  
                        top=temp;  
                        top->link=NULL;  
                        top->blink=NULL;  
                      }  
                      else  
                      {  
                        start->blink=temp;  
                        temp->link=start;  
                        start=temp;  
                        start->blink=NULL;  
                      }  
                      printf("nEnter your data:n");  
                      scanf("%d",&(start->data));  
               }  
               else if(ch=='2')  
               {  
                      top->link=temp;  
                      temp->blink=top;  
                      top=temp;  
                      top->link=NULL;  
                      printf("nEnter your data:n");  
                      scanf("%d",&(top->data));  
               }  
               else if(ch=='3')  
               {  
                      printf("nntEnter the index at which you want to enter the data:");  
                      scanf("%d",&index);  
                      tptr=start;  
                      while(tptr!=NULL)  
                      {  
                        i++;  
                        if(i==index)  
                        {  
                               temp->blink=tptr;  
                               temp->link=tptr->link;  
                               tptr->link=temp;  
                               printf("nntEnter your element you want to insert:");  
                               scanf("%d",&(temp->data));  
                               break;  
                        }  
                        else  
                        {  
                               tptr=tptr->link;  
                        }  
                      }  
                      if(tptr==NULL)  
                      {  
                        printf("nnElement you enter is not exitst in link list");  
                      }  
               }  
               else  
               {  
                      printf("Invalid choice");  
               }  
               printf("nntYou want to insert more element(Y/N):");  
             }while((getch())=='y');  
      }  
      void show()  
      {  
             node *temp;  
             if(start==NULL)  
             {  
                    printf("nyour link list is emptyn");  
             }  
             else  
             {  
               printf("nnYour link list is: ");  
               temp=start;  
               while(temp!=NULL)  
               {  
                      printf("%d ",temp->data);  
                      temp=temp->link;  
               }  
               printf("n");  
             }  
      }  
      void delet()  
      {  
             int index,i=0;  
             node *temp1,*temp2;  
             printf("nntEnter the index of node you want to delete:");  
             scanf("%d",&index);  
             temp1=start;  
             while(temp1!=NULL)  
             {  
               i++;  
               if(i==index)  
               {  
                      temp2=temp1->blink;  
                      temp2->link=temp1->link;  
                      temp1=temp1->link;  
                      temp1->blink=temp2;  
                      break;  
               }  
               else  
               {  
                      temp1=temp1->link;  
               }  
             }  
             if(temp1!=NULL)  
             printf("nelement deleted successfully!!!n");  
             else  
           {  
                  printf("nnIndex is out of bound please try again!!!");  
           }  
      }  
      void search()  
      {  
             int index,i=0;  
             node *tptr;  
             printf("nntEnter the index at which you want to search the data:");  
             scanf("%d",&index);  
             tptr=start;  
             while(tptr!=NULL)  
             {  
               i++;  
               if(i==index)  
               {  
                      printf("nThe element at %d index is:=%d",index,tptr->data);  
                      break;  
               }  
               else  
               {  
                      tptr=tptr->link;  
               }  
             }  
             if(tptr==NULL)  
             {  
               printf("nnIndex is out of bound please try again!!!");  
             }  
      }  

No comments:

Post a Comment