Pages

Tuesday, 27 August 2013

Concatenation of Linked List Alternatively

Hi, This program is about implementation of Concatenation of linked list alternatively

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 concate_alternative();   
         typedef struct node   
         {   
           int data;   
           struct node *link;   
         }node;  
         node *first1=NULL,*last1,*first2=NULL,*last2;   
         void main()   
         {   
           int i=0;   
           node *temp;   
           printf("Enter the data in first link list");   
           do   
           {   
                  clrscr();   
                   temp=(node*)malloc(sizeof(node));   
                  i++;   
                  if(first1==NULL)   
                  {   
                     first1=temp;   
                     last1=temp;   
                     last1->link=NULL;   
                  }   
                  else   
                  {   
                     last1->link=temp;   
                     last1=temp;   
                     last1->link=NULL;   
                  }   
                   printf("\nEnter the %d element for first linked list: ",i);   
                   scanf("%d",&(last1->data));   
                   printf("\nYou want to enter more data(Y/N)");   
           }while(getch()=='y');  
           printf("\n\n\nYour first link list is:");   
           temp=first1;   
           while(temp!=NULL)   
           {   
                   printf("%d ",temp->data);   
              temp=temp->link;   
           }   
           getch();   
      //===========Scan data for second linked list=============   
           i=0;   
           clrscr();   
           printf("\n\nEnter the data in second link list");   
           do   
           {   
                   clrscr();   
                   temp=(node*)malloc(sizeof(node));   
                  i++;   
                   if(first2==NULL)   
                   {   
                    first2=temp;   
                    last2=temp;   
                    last2->link=NULL;   
                   }   
                   else   
                   {   
                    last2->link=temp;   
                    last2=temp;   
                    last2->link=NULL;   
                   }   
                  printf("\nEnter the %d element for second linked list: ",i);   
                  scanf("%d",&(last2->data));   
                  printf("\nYou want to enter more data(Y/N)");   
            }while(getch()=='y');   
            printf("\n\n\nYour second link list is:");   
            temp=first2;   
            while(temp!=NULL)   
            {   
                  printf("%d ",temp->data);   
                  temp=temp->link;   
            }   
            concate_alternative();   
            getch();   
      }   
         void concate_alternative()   
         {   
           node *temp1,*temp2,*first3=NULL,*last3,*temp;   
            int i=0;   
            temp1=first1;   
            temp2=first2;   
            while(temp1!=NULL && temp2!=NULL)   
            {   
                   if(i%2==0)   
                   {   
                      if(first3==NULL)   
                      {   
                           first3=temp1;   
                           last3=temp1;   
                      }   
                      else   
                      {   
                           last3->link=temp1;   
                           last3=temp1;   
                      }   
                      temp1=temp1->link;   
                      i++;   
                   }   
                   else   
                   {   
                      if(first3==NULL)   
                      {   
                           first3=temp2;   
                           last3=temp2;   
                      }   
                      else   
                      {   
                         last3->link=temp2;   
                         last3=temp2;   
                      }   
                      temp2=temp2->link;   
                    i++;   
                   }   
       }   
       if(temp1==NULL)   
       {   
             last3->link=temp2;   
       }   
       else if(temp2==NULL)   
       {   
             last3->link=temp1;   
       }   
       else   
       {   
             printf("\n\nCode is not working properly");   
       }   
       printf("\n\nYour final list is:");   
       temp=first3;   
       while(temp!=NULL)   
       {   
             printf("%d ",temp->data);   
         temp=temp->link;   
       }    
    }   

Monday, 26 August 2013

Reverse a Singly Linked List

Hi, This program is about implementation of Reverse a singly linked list.

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 reverse();  
      typedef struct node  
      {  
            int data;  
            struct node *link;  
      }node;  
      node *first=NULL,*last;  
      void main()  
      {  
           int i=0;  
           node *temp;  
           printf("Enter the link list data");  
            do  
            {  
                 clrscr();  
                  temp=(node*)malloc(sizeof(node));  
                 i++;  
                 if(first==NULL)  
                 {  
                        first=temp;  
                        last=temp;  
                        last->link=NULL;  
                 }  
                 else  
                 {  
                        last->link=temp;  
                        last=temp;  
                        last->link=NULL;  
                 }  
                  printf("\nEnter the %d element",i);  
                  scanf("%d",&(last->data));  
                  printf("\nYou want to enter more data(Y/N)");  
            }while(getch()=='y');  
            printf("\n\nYour link list is:");  
            temp=first;  
            while(temp!=NULL)  
            {  
                  printf("%d ",temp->data);  
                  temp=temp->link;  
            }  
            checkloop();  
      }  
      void checkloop()  
      {  
            int flag=0;  
            node *fast,*slow;  
            fast=first;  
            slow=first;  
            while(fast!=NULL && slow!=NULL)  
            {  
                   fast=(fast->link)->link;  
                   slow=slow->link;  
                   if(fast==slow)  
                   {  
                    flag=1;  
                   }  
            }  
            if(flag=1)  
           {  
                   printf("\nlink list contains loop");  
           }  
            else  
           {  
                   printf("\nloop nthi vo bhai");  
           }  
            temp=first;  
            first=last;  
            last=temp;  
            ptr=first;  
            if(ptr==NULL)  
            {  
                  printf("empty");  
            }  
            while(ptr!=NULL)  
            {  
                  printf("%d ",ptr->data);  
                  ptr=ptr->link;  
            }  
            getch();  
      }.   

Detect And Remove Loop in Singly linked List in C

Hi This program is about implementation of Detect and solve looping problem in singly linked list.

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>  
      typedef struct node  
      {  
            int data;  
            struct node *link;  
      }node;  
      node *first=NULL,*last;  
      void solve(node *,node *);  
      void checkloop();  
      void main()  
      {  
           int i=0;  
           node *temp;  
            printf("Enter the link list data");  
            do  
            {  
                 clrscr();  
                  temp=(node*)malloc(sizeof(node));  
                 i++;  
                 if(first==NULL)  
                 {  
                        first=temp;  
                        last=temp;  
                        last->link=NULL;  
                 }  
                 else  
                 {  
                        last->link=temp;  
                        last=temp;  
                        last->link=NULL;  
                 }  
                       last->link=first;  
                       printf("\nEnter the %d element",i);  
                       scanf("%d",&(last->data));  
                       printf("\nYou want to enter more data(Y/N)");  
                 }while(getch()=='y');  
                 checkloop();  
           }  
      void checkloop()  
      {  
                 int flag=0;  
                 node *fast,*slow,*temp;  
                 fast=first;  
                 slow=first;  
                 while(fast!=NULL && slow!=NULL)  
                 {  
                        fast=(fast->link)->link;  
                        slow=slow->link;  
                        if(fast==slow)  
                        {  
                         flag=1;  
                         break;  
                        }  
                 }  
                 if(flag==1)  
                 {  
                       printf("\n\n\tlink list contains loop");  
                       solve(fast,slow);  
                 }  
                 else  
                        printf("\nloop nthi vo bhai");  
                 getch();  
      }  
      void solve(node *fast,node *slow)  
      {  
            int i=1,j;  
            node *temp;  
            fast=fast->link;  
            while(fast!=slow)  
            {  
                   fast=fast->link;  
               i++;  
            }  
            fast=slow=first;  
            for(j=0;j<i;j++)  
            {  
                  fast=fast->link;  
       }  
       while(fast!=slow)  
       {  
            fast=fast->link;  
            slow=slow->link;  
       }  
       for(j=0;j<i-1;j++)  
       {  
             fast=fast->link;  
       }  
            fast->link=NULL;  
            printf("\n\n\nafter solving looping problem Your list is:");  
            temp=first;  
            while(temp!=NULL)  
            {  
                   printf("%d ",temp->data);  
                   temp=temp->link;  
            }  
      }  

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!!!");  
          }  
      }   

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!!!");  
             }  
      }  

Friday, 23 August 2013

Singly Linked List Program in C

Hi, This program is about implementation of Singly 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>  
      struct node  
      {  
             int data;  
             struct node *link;  
      };  
      struct node *start=NULL,*top=NULL,*temp=NULL;  
      int i=0;  
      void print();  
      void create();  
      void search();  
      void delet();  
      void show();  
      void lin(int n)  
      {  
             int i;  
             printf("n");  
             for(i=0;i<n;i++)  
             {  
               printf("*");  
             }  
             printf("n");  
      }  
      void main()  
      {  
             clrscr();  
             print();  
      }  
      void print()  
      {  
             clrscr();  
             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:");  
             switch(getch())  
             {  
               case '1':  
                    create();  
                    break;  
               case '2':  
                    delet();  
                    break;  
               case '3':  
                    search();  
                    break;  
               case '4':  
                    show();  
                    break;  
               default:  
                    printf("nInvalid choice!!!!n");  
             }  
             printf("nYou want to do more operation.(Y/N):n");  
             if(getch()=='y')  
             print();  
      }  
      void create()  
      {  
             do  
             {  
               temp=(struct node*)malloc(sizeof(struct node));  
               if(start==NULL)  
               {  
                      start=temp;  
                      top=temp;  
                      top->link=NULL;  
               }  
               else  
               {  
                      top->link=temp;  
                      top=temp;  
                      top->link=NULL;  
               }  
               lin(35);  
               printf("nEnter the %d element: ",i+1);  
                     i++;  
               scanf("%d",&(top->data));  
               lin(40);  
               printf("nyou want to enter more elements(Y/N):n");  
             }while(getch()=='y');  
      }  
      void delet()  
      {  
             int num;  
             struct node *temp1=NULL;  
             printf("nEnter the data in which you want to delete the node");  
             scanf("%d",&num);  
             temp=start;  
             while(temp!=NULL)  
             {  
               if(temp->data==num)  
               {  
                      i--;  
                      if(temp->link==NULL)  
                      {  
                        temp1->link=temp->link;  
                        top=temp1;  
                        break;  
                      }  
                      else if(temp==start)  
                      {  
                        start=NULL;  
                        break;  
                      }  
                      else  
                      {  
                        temp1->link=temp->link;  
                        break;  
                      }  
               }  
               else  
               {  
                      temp1=temp;  
                      temp=temp->link;  
               }  
             }  
      }  
      void search()  
      {  
             int i=0,cnt=1;  
             printf("nnEnter the index in which you want to search a node in list:");  
             scanf("%d",&i);  
             temp=start;  
             while(cnt!=i)  
             {  
               temp=temp->link;  
               cnt++;  
             }  
             printf("nYour node at index %d is:= %d",i,temp->data);  
      }  
      void show()  
      {  
             if(start==NULL)  
             {  
               printf("your link list is emptyn");  
             }  
             printf("nnYour link list is: ");  
             temp=start;  
             while(temp!=NULL)  
             {  
               printf("%d ",temp->data);  
               temp=temp->link;  
             }  
             printf("n");  
      }