Thursday, April 26, 2012

Merging two LinkList.





#include <stdio.h>
#include <conio.h>

//Merging two List
  typedef struct node
      {
           int n;
           struct node *next;
      }List;

  void Insert(List **first)
      {
           List *temp,*cur;
           temp=(List *)malloc(sizeof(List));
           printf("\n\tEnter the Value : ");
           scanf("%d",&temp->n);
           if(*first==NULL)
               {
                    *first=cur=temp;
                    temp->next=NULL;
               }
           else
               {
                    for(cur=*first;cur->next!=NULL;cur=cur->next);
                    cur->next=temp;
                    temp->next=NULL;
               }
      }

  void disp(List *first)
      {
           List *cur;
           for(cur=first;cur!=NULL;cur=cur->next)
               {
                    printf("%d ",cur->n);
               }
           getch();
           printf("\n");
      }

      void merge(List *first,List **final)
           {
               List *cur,*temp,*pr;
               for(cur=first;cur!=NULL;cur=cur->next)
                    {
                         temp=(List *)malloc(sizeof(List));
                         temp->n=cur->n;
                         temp->next=NULL;
                         if(*final==NULL)
                             {
                                  *final=temp;
                             }
                         else
                             {
                                  for(pr=*final;pr->next!=NULL;pr=pr->next);
                                  pr->next=temp;
                             }
                    }
           }

  void main()
      {
           int c=0,ch=0;
           List *head,*head1,*head2;
           clrscr();
           head=head1=head2=NULL;
           while(c!=4)
               {
                    clrscr();
                    fflush(stdin);
                    printf("\n\n\t1. Add");
                    printf("\n\t2. Display");
                    printf("\n\t3. Merge");
                    printf("\n\t4. Exit");
                    printf("\n\tEnter your choice : ");
                    scanf("%d",&c);
                    fflush(stdin);
                    switch(c)
                         {
                             case 1:
                                  while(ch!=3)
                                       {
                                             printf("\n\t1. List-1");
                                             printf("\n\t2. List-2");
                                             printf("\n\t3. Exit");
                                             printf("\n\tEnter Choice : ");
                                             scanf("%d",&ch);
                                             switch(ch)
                                                  {
                                                      case 1:      Insert(&head);break;
                                                      case 2:      Insert(&head1);break;
                                               }
                                             fflush(stdin);
                                       }
                                             break;
                             case 2:
                                  ch=0;
                                  while(ch!=3)
                                       {
                                             printf("\n\t1. List-1");
                                             printf("\n\t2. List-2");
                                             printf("\n\t3. List-3");
                                             printf("\n\tEnter Choice : ");
                                             scanf("%d",&ch);
                                             fflush(stdin);
                                             switch(ch)
                                             {
                                                  case 1:  printf("\n\tFirst List : ");
                                                                      disp(head);break;
                                                  case 2:  printf("\n\tSecond List : ");
                                                                      disp(head1);break;
                                                  case 3:  printf("\n\tMerged List : ");
                                                                      disp(head2);break;
                                             }
                                    }
                                             break;
                             case 3:      merge(head,&head2);
                                             merge(head1,&head2);
                                             break;
                             case 4:      printf("\n\tQuit....");
                                             getch();
                                             exit(0);
                             default:printf("\nWrong no");
                         }
               }
           getch();
      }



Thursday, April 19, 2012

Circular LinkList



A circular linked list is a Linear Linklist whose last node point back to the head node.


#include <stdio.h>
#include <conio.h>

  typedef struct Clist
      {
           int n;
           struct Clist *next;
      }node;
      node *head;

  void Insert(int num)
      {
           node *pr,*temp;
           temp= (node *)malloc(sizeof(node));
           temp->n = num;
           if(head == NULL)
              {
                   head = temp;
                   temp->next = head;
              }
           else
              {
                   temp->next = head->next;
                   head->next = temp;
                   head = temp;
              }
      }

  void InsertBeg(int num)
      {
           node *temp;
           temp=(node *)malloc(sizeof(node));
           temp->n=num;
           temp->next=head->next;
           head->next=temp;
      }

  void InsetAft(int num,int pos)
      {
           node *temp,*pr;
           int i=0;
           pr=head->next;
           while(pr!=NULL)
              {
                   pr=pr->next;
                   if(pr==head->next)
                        {
                            printf("\n\tPosition %d is Out of Range!!",pos);
                            return;
                        }
                   i++;
                   if(i==pos)
                        {
                            break;
                        }
              }
           temp =(node *)malloc(sizeof(node) );
           temp->next = pr->next;
           temp->n = num;
           pr->next = temp;
           if(pr==head)
              {
                   head=temp;
              }
      }

  void del(int num)
      {
           node *temp,*pr;
           if( head->next == head && head->n == num)
              {
                   temp = head;
                   head = NULL;
                   free(temp);
                   return;
              }
           pr = head->next;
           if(pr->n == num)
              {
                   temp = pr;
                   head->next = pr->next;
                   free(temp);
                   return;
              }
           while(pr->next != head)
              {
                   if(pr->next->n == num)
                        {
                            temp = pr->next;
                            pr->next = temp->next;
                            free(temp);
                            printf("\n\tThe No. %d is Deleted\n",num);
                            return;
                        }
                   pr = pr->next;
              }
           if(pr->next->n == num)
              {
                   temp = pr->next;
                   pr->next = head->next;
                   free(temp);
                   head = pr;
                   return;
              }
           printf("\n\t %d is Not Present\n",num);
           getch();
      }

  void Disp()
      {
           node *pr;
           if(head == NULL)
              {
                   printf("\n\tList is Undeflow");
                   return;
              }
           pr = head->next;
           printf("\n\tValues in the List : ");
           while(pr != head)
              {
                   printf("%d - ", pr->n);
                   pr = pr->next;
              }
           printf("%d\n",head->n);
           getch();
      }

  void main()
      {
           int ch=0,num,pos;
           head=NULL;
           while(ch!=6)
              {
                   clrscr();
                   printf("\n\t1. Insert");
                   printf("\n\t2. Display");
                   printf("\n\t3. Insert at Begining");
                   printf("\n\t4. Insert After");
                   printf("\n\t5. Delete");
                   printf("\n\t6. Exit");
                   printf("\n\tEnter Choice : ");
                   scanf("%d",&ch);
                   switch(ch)
                        {
                            case 1:
                                printf("\n\tEnter an Element : ");
                                scanf("%d",&num);
                                Insert(num);
                                break;
                            case 2:
                                if(head == NULL)
                                      {
                                                printf("\n\tList is Empty!\n");
                                                getch();
                                       }
                                else
                                      {
                                                Disp();
                                      }
                                                break;
                            case 3:
                                printf("\n\tEnter an Element : ");
                                scanf("%d",&num);
                                InsertBeg(num);
                                break;
                            case 4:
                                printf("\n\tEnter an Element : ");
                                scanf("%d",&num);
                                printf("\n\tEnter the Position of Insertion : ");
                                scanf("%d",&pos);
                                InsetAft(num,pos);
                                break;
                            case 5:
                                if(head == NULL)
                                      {
                                                printf("\n\tUnderflow Condition Occured\n");
                                                getch();
                                      }
                                else
                                      {
                                                printf("\n\tEnter the Number to Delete : ");
                                                scanf("%d",&num);
                                                del(num);
                                      }
                                                break;
                            case 6:
                                printf("\n\tQuit..........");
                                getch();
                                exit(0);
                            default:
                                printf("\n\tThe Choice is Out of Rangen");
                        }
              }
      }

Thursday, April 12, 2012

LinkList – Concatenation of Two List



#include <stdio.h>
#include <conio.h>
  //Concatenation of two List
  typedef struct node
      {
           int n;
           struct node *next;
      }List;
  List *ptr;
  void Insert(List **first)
      {
           List *temp,*cur;
           if(ptr!=NULL)
              {
                   ptr->next=NULL;
                   printf("\n\tClearing Concatenate Data from First List\n");
              }
           temp=(List *)malloc(sizeof(List));
           printf("\n\tEnter the Value : ");
           scanf("%d",&temp->n);
           if(*first==NULL)
              {
                   *first=cur=temp;
                   temp->next=NULL;
              }
           else
              {
                   for(cur=*first;cur->next!=NULL;cur=cur->next);
                   cur->next=temp;
                   temp->next=NULL;
              }
      }

  void disp(List *first)
      {
           List *cur;
           for(cur=first;cur!=NULL;cur=cur->next)
              {
                   printf("%d ",cur->n);
              }
           getch();
           printf("\n");
      }

      void concat(List *first,List *second)
           {
              List *cur;
              ptr=NULL;
              printf("\n\tSecond List Concatenate with First : \n");
              for(cur=first;cur->next!=NULL;cur=cur->next);
              ptr=cur;
              cur->next=second;
           }

  void main()
      {
           int c=0,ch=0;
           List *head,*head1;
           clrscr();
           head=head1=ptr=NULL;
           while(c!=4)
              {
                   clrscr();
                   fflush(stdin);
                   printf("\n\n\t1. Add");
                   printf("\n\t2. Display");
                   printf("\n\t3. Concat");
                   printf("\n\t4. Exit");
                   printf("\n\tEnter your choice : ");
                   scanf("%d",&c);
                   fflush(stdin);
                   switch(c)
                        {
                            case 1:
                                ch=0;
                                while(ch!=3)
                                    {
                                         printf("\n\t1. List-1");
                                         printf("\n\t2. List-2");
                                         printf("\n\t3. Exit");
                                         printf("\n\tEnter Choice : ");
                                         scanf("%d",&ch);
                                         switch(ch)
                                              {
                                                  case 1:       Insert(&head);break;
                                                  case 2:       Insert(&head1);break;
                                           }
                                         fflush(stdin);
                                    }
                                         break;
                            case 2:
                                ch=0;
                                while(ch!=3)
                                    {
                                         printf("\n\t1. List-1");
                                         printf("\n\t2. List-2");
                                         printf("\n\t3. Exit");
                                         printf("\n\tEnter Choice : ");
                                         scanf("%d",&ch);
                                         fflush(stdin);
                                         switch(ch)
                                         {
                                              case 1:  printf("\n\tFirst List : ");
                                                            disp(head);break;
                                              case 2:  printf("\n\tSecond List : ");
                                                            disp(head1);break;
                                         }
                                  }
                                         break;
                            case 3:   concat(head,head1);
                                         printf("\n\tFirst List, After Concat : ");
                                         disp(head);break;
                            case 4:   printf("\n\tQuit....");
                                         getch();
                                         exit(0);
                            default:printf("\nWrong no");
                        }
              }
           getch();
      }