Thursday, April 5, 2012

Set Operation Using Linklist




Set Theory, branch of mathematics, first given formal treatment by the German mathematician George Cantor in the 19th century. A set is an aggregate, class, or collection of objects, which are called the elements of the set. If A and B are two subsets of a set S, the elements found in A or in B or in both form a subset of S called the union of A and B. The elements common to A and B form a subset of S called the intersection of A and B. If the set notation is A- B, A minus B or A compliment B, it means everything in A except for anything in its overlap with B or vice-versa.



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


  typedef struct node
      {
           int n;
           struct node *next;
      }List;
  List *ptr;
  void Insert(List **first,int num)
      {
           List *temp,*cur;
           temp=(List *)malloc(sizeof(List));
           temp->n=num;
           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;
              }
      }
  int chknum(List *first,int num)
      {
           List *cur;
           for(cur=first;cur!=NULL;cur=cur->next)
              {
                   if(cur->n==num)
                       {
                            printf("\n\tDuplicate Entry!");
                            return 0;
                       }
              }
           return 1;

      }

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

      void intersect(List *first,List *second,List **Inter)
           {
              int f;
              List *cur,*pr;
              for(cur=first;cur!=NULL;cur=cur->next)
                   {
                       f=0;
                       for(pr=second;pr!=NULL;pr=pr->next)
                            {
                                if(pr->n==cur->n)
                                     {
                                          f=1;
                                          break;
                                     }
                            }
                            if(f==1)
                                {
                                     Insert(&(*Inter),pr->n);
                                }
                   }

           }
      void minus(List *first,List *second,List **mns)
           {
              int f;
              List *cur,*pr;
              for(cur=first;cur!=NULL;cur=cur->next)
                   {
                       f=0;
                       for(pr=second;pr!=NULL;pr=pr->next)
                            {
                                if(pr->n==cur->n)
                                     {
                                          f=1;
                                          break;
                                     }
                            }
                            if(f==0)
                                {
                                     Insert(&(*mns),cur->n);
                                }
                   }

           }

      void Unions(List *first,List *second,List **Uni)
           {
              int f;
              List *cur,*pr;
              for(cur=first;cur!=NULL;cur=cur->next)
                   {
                       Insert(&(*Uni),cur->n);

                   }

              for(cur=second;cur!=NULL;cur=cur->next)
                   {
                       f=0;
                       for(pr=first;pr!=NULL;pr=pr->next)
                            {
                                if(pr->n==cur->n)
                                     {
                                          f=1;

                                          break;
                                     }
                            }
                       if(f==0)
                            {
                                Insert(&(*Uni),cur->n);
                            }
                   }
           }

  void main()
      {
           int c=0,ch=0,num,f;
           List *head,*head1,*head2,*head3,*head4,*head5;
           clrscr();
           head=head1=head2=head3=head4=head5=NULL;
           while(c!=6)
              {
                   clrscr();
                   fflush(stdin);
                   printf("\n\n\t1. Add");
                   printf("\n\t2. Display");
                   printf("\n\t3. Intersect");
                   printf("\n\t4. Union");
                   printf("\n\t5. Minus");
                   printf("\n\t6. Exit");
                   printf("\n\tEnter your choice : ");
                   scanf("%d",&c);
                   fflush(stdin);
                   switch(c)
                       {
                            case 1:
                                ch=0;
                                f=0;
                                while(ch!=3)
                                     {
                                          printf("\n\t1. Set-1");
                                          printf("\n\t2. Set-2");
                                          printf("\n\t3. Exit");
                                          printf("\n\tEnter Choice : ");
                                          scanf("%d",&ch);
                                          switch(ch)
                                               {
                                                    case 1:  printf("\n\tEnter the Value : ");
                                                                   scanf("%d",&num);
                                                                   if(chknum(head,num)==1)
                                                                             {
                                                                                      Insert(&head,num);
                                                                             }
                                                                   break;
                                                    case 2:      printf("\n\tEnter the Value : ");
                                                                   scanf("%d",&num);
                                                                   if(chknum(head1,num)==1)
                                                                             {
                                                                                      Insert(&head1,num);
                                                                             }
                                                                   break;
                                               }
                                          fflush(stdin);
                                     }
                                          break;
                            case 2:
                                ch=0;
                                while(ch!=3)
                                     {
                                          printf("\n\t1. Set-1");
                                          printf("\n\t2. Set-2");
                                          printf("\n\t3. Exit");
                                          printf("\n\tEnter Choice : ");
                                          scanf("%d",&ch);
                                          fflush(stdin);
                                          switch(ch)
                                          {
                                               case 1:  printf("\n\tFirst Set : ");
                                                            disp(head);break;
                                               case 2:  printf("\n\tSecond Second : ");
                                                            disp(head1);break;
                                          }
                                  }
                                          break;
                            case 3:  head2=NULL;
                                          intersect(head,head1,&head2);
                                          printf("\n\tFirst List, After Intersect : ");
                                          disp(head2);break;
                            case 4:  head3=NULL;
                                          Unions(head,head1,&head3);
                                          printf("\n\tFirst List, After Union : ");
                                          disp(head3);break;
                            case 5:  head4=head5=NULL;
                                          minus(head,head1,&head4);
                                          printf("\n\tFirst List, A - B : ");
                                          disp(head4);
                                          minus(head1,head,&head5);
                                          printf("\n\tFirst List, B - A : ");
                                          disp(head5);break;
                            case 6:    printf("\n\tQuit....");
                                          getch();
                                          exit(0);
                            default:printf("\n\tWrong no");
                       }
              }
           getch();
      }



No comments: