Pages

Monday, 26 August 2013

Circular Doubly Linked List Program in C

Hi, This program is about implementation of Circular 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,*lastnode=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("\n\n\tYou 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 biggining\n");  
                 printf("\n2.insert at the end\n");  
                 printf("\n3.insert at specified index\n");  
                 lin(50);  
                 printf("\nChoose you operation:\n");  
                 ch=getch();  
                 if(ch=='1')  
                 {  
                        if(start==NULL)  
                        {  
                          start=temp;  
                          lastnode=temp;  
                          lastnode->link=temp;  
                          lastnode->blink=temp;  
                        }  
                        else  
                        {  
                         start->blink=temp;  
                         temp->link=start;  
                         start=temp;  
                         start->blink=lastnode;  
                         lastnode->link=start;  
                        }  
                        printf("\nEnter your data:\n");  
                        scanf("%d",&(start->data));  
                 }  
                 else if(ch=='2')  
                 {  
                        lastnode->link=temp;  
                        temp->blink=lastnode;  
                        lastnode=temp;  
                        lastnode->link=start;  
                        start->blink=lastnode;  
                        printf("\nEnter your data:\n");  
                        scanf("%d",&(lastnode->data));  
                 }  
                  else if(ch=='3')  
                  {  
                         printf("\n\n\tEnter the index at which you want to enter the data:");  
                         scanf("%d",&index);  
                         tptr=start;  
                    while(tptr->link!=start)  
                    {  
                           i++;  
                          if(i==index)  
                          {  
                              temp->blink=tptr;  
                              temp->link=tptr->link;  
                              tptr->link=temp;  
                              (tptr->link)->blink=temp;  
                              printf("\n\n\tEnter your element you want to insert:");  
                              scanf("%d",&(temp->data));  
                              break;  
                          }  
                          else  
                          {  
                                 tptr=tptr->link;  
                          }  
                    }  
                    if(tptr->link==start)  
                    {  
                           printf("\n\nIndex is out of bound!!");  
                    }  
                  }  
                 else  
                 {  
                        printf("Invalid choice");  
                 }  
                 printf("\n\n\tYou want to insert more element(Y/N):");  
           }while((getch())=='y');  
      }  
      void show()  
      {  
            node *temp;  
             if(start==NULL)  
             {  
               printf("\nyour link list is empty\n");  
             }  
             else  
             {  
               printf("\n\nYour link list is: ");  
               temp=start;  
                 while(temp->link!=start)  
                 {  
                      printf("%d ",temp->data);  
                      temp=temp->link;  
                 }  
               printf("%d\n",temp->data);  
             }  
      }  
      void delet()  
      {  
            int index,i=0,flag=0;  
            node *temp1,*temp2;  
            printf("\n\n\tEnter the index of node you want to delete:");  
            scanf("%d",&index);  
            temp1=start;  
            while(temp1->link!=start)  
            {  
                 i++;  
                if(i==index)  
                {  
                       if(temp1!=start)  
                      {  
                            flag=1;  
                            temp2=temp1->blink;  
                            temp2->link=temp1->link;  
                            temp1=temp1->link;  
                            temp1->blink=temp2;  
                      }  
                      else  
                      {  
                            flag=1;  
                            if(lastnode==start)  
                            {  
                                  start=NULL;  
                                  lastnode=NULL;  
                            }  
                            else  
                            {  
                                 temp2=temp1->blink;  
                                 temp2->link=temp1->link;  
                                 start=temp1->link;  
                                 temp1=temp1->link;  
                                 temp1->blink=temp2;  
                            }  
                      }  
                       break;  
                }  
                else  
                {  
                      temp1=temp1->link;  
                }  
            }  
            if(flag==1)  
           {  
                  printf("\nElement deleted successfully!!!\n");  
           }  
            else  
            {  
                  i++;  
                  if(i==index)  
                  {  
                      if(lastnode==start)  
                       {  
                             start=NULL;  
                             lastnode=NULL;  
                       }  
                       else  
                       {  
                            temp2=temp1->blink;  
                            lastnode=temp1->blink;  
                            temp2->link=temp1->link;  
                            temp1=temp1->link;  
                            temp1->blink=temp2;  
                       }  
                    printf("\nElement deleted successfully!!!\n");  
                  }  
                  else  
                  {       
                        printf("\n\nIndex is out of bound please try again!!!");  
                  }  
            }  
      }  
      void search()  
      {  
            int index,i=0;  
            node *tptr;  
            printf("\n\n\tEnter the index at which you want to search the data:");  
               scanf("%d",&index);  
               tptr=start;  
          while(tptr->link!=start)  
          {  
                 i++;  
                if(i==index)  
                {  
                       printf("\nThe element at %d index is:=%d",index,tptr->data);  
                       break;  
                }  
                else  
                {  
                       tptr=tptr->link;  
                }  
          }  
          if(tptr->link==start)  
          {  
                 printf("\n\nIndex is out of bound please try again!!!");  
          }  
      }   

No comments:

Post a Comment