Loading

Archive for 3 Oct 2012

C - Program to Implement Various Set Operations - Union, Intesection

C - Program to Implement Various Set Operations

/*  Operations covered :
      1) Create()     : for creating a new set with initial members
                of the set
      2) print()      : diaplays all members of the set
      3) Union()      : finds union of two sets, set1[] and set2[]  and
                stores the result in set3[]
      4) intersection() : finds intersection of two sets, set1[] and set2[]
                and stores the result in set3[]
      5) difference() :finds difference of two sets, set1[] and set2[]
               and stores the result in set3[]
      6) member()     :function returns 1 or 0 ,depending onwhether the
              element  x belongs or not to a  set.

      7) symmdiff()  : Finds Symmetric difference of two sets
Representation of a set
-----------------------
      A set is representrd using an  array of integers.
      It may be declared as:
           int set[30];
      set[0] - gives number of elements in a set.
      set[1] to set[29] are for storing set members.

      Example :
           A set,[2,11,3,5 6],when represebted will appear as:
           [5][2][3][5][6][11][ ][ ][ ] <--- array set[]
            0  1  2  3  4  5   6  7  8  <--- index
*/

#define MAX 30
#include<stdio.h>
#include<conio.h>
void create(int set[]);
void print(int set[]);
void Union(int set1[],int set2[],int set3[]);
void intersection(int set1[],int set2[],int set3[]);
void difference(int set1[],int set2[],int set3[]);
void symmdiff(int set1[],int set2[],int set3[]);
int member(int set[],int x);


void main()
{ int set1[MAX],set2[MAX],set3[MAX];
  int x,op;
  clrscr();
  flushall();
  set1[0]=set2[0]=set3[0]=0;
  do
   { printf("\n1)Create\n2)Print\n3)Union\n4)Intersection\n5)Difference");
     printf("\n6Symmetrec Difference \n7)Quit");
     printf("\nEnter Your Choice:");
     scanf("%d",&op);
     switch(op)
      {
    case 1: printf("\nCreting First Set*******");
        create(set1);
        printf("\nCreating Second Set*****");
        create(set2);
        break;
    case 2: printf("\nFirst Set :\n");
        print(set1);
        printf("\n\nSecond Set :\n");
        print(set2);
        printf("\n\nThird Set :\n");
        print(set3);
        break;
    case 3: Union(set1,set2,set3);print(set3);break;
    case 4: intersection(set1,set2,set3);print(set3);break;
    case 5: difference(set1,set2,set3);print(set3);break;
    case 6: symmdiff(set1,set2,set3);print(set3);break;
     }
  printf("\npress a key............");
  getch();
  }while(op!=7);
 }

 /*creates set[] with initial elements*/

 void create(int set[])
   {   int n,i,x;
       set[0]=0;/*make it a null set*/
       printf("\n No. of elements in the set:");
       scanf("%d",&n);
       printf("\n enter set elements :");
       for(i=1;i<=n;i++)
       scanf("%d",&set[i]);
       set[0]=n; //Number of elements.

   }

 void  print(int set[])
  { int i,n;
    n=set[0];/* number of elements in the set */
    printf("\Members of the set :-->");
    for(i=1;i<=n;i++)
       printf("%d  ",set[i]);
  }

 /* union of  set1[] and set2[] is stored in set3[]*/

void Union(int set1[],int set2[],int set3[])
  { int i,n;
    /* copy set1[] to set3[]*/
    set3[0]=0;/*make set3[] a null set */
    n=set1[0];/* number of elements in the set*/
    //Union of set1,set2= set1 + (set2-set1)
    for(i=0;i<=n;i++)
    set3[i]=set1[i];

    n=set2[0];
    for(i=1;i<=n;i++)
       if(!member(set3,set2[i]))
        set3[++set3[0]]=set2[i];  // insert and increment no. of elements
   }

 /*function returns 1 or 0 depending on whether x belongs
  to set[] or not */

 int member(int set[],int x)
  { int i,n;
    n=set[0]; /* number of elements in the set*/
    for(i=1;i<=n;i++)
      if(x==set[i])
     return(1);

     return(0);
  }

/*intersection of set1[] and set2[] is stored in set3[]*/

void intersection(int set1[],int set2[],int set3[])
     {
    int i,n;
    set3[0]=0; /* make a NULL set*/
    n=set1[0];/* number of elements in the set*/
    for(i=1;i<=n;i++)
      if(member(set2,set1[i])) /* all common elements are inserted in set3[]*/
           set3[++set3[0]]=set1[i];  // insert and increment no. of elements
     }

/*difference of set1[] and set2[] is stored in set3[]*/

void difference(int set1[],int set2[],int set3[])
      { int i,n;
    n=set1[0];/* number of elements in the set*/
    set3[0]=0;/*make it a null set*/
    for(i=1;i<=n;i++)
       if(!member(set2,set1[i]))
         set3[++set3[0]]=set1[i];  // insert and increment no. of elements
      }

 void symmdiff(int set1[],int set2[],int set3[])
      { int i,n;
    n=set1[0];/* number of elements in the set*/
    set3[0]=0;/*make it a null set*/
    //Calculate set1-set2
    for(i=1;i<=n;i++)
       if(!member(set2,set1[i]))
         set3[++set3[0]]=set1[i];  // insert and increment no. of elements
    //Calculate set2-set1
    n=set2[0];
    for(i=1;i<=n;i++)
       if(!member(set1,set2[i]))
         set3[++set3[0]]=set2[i];  // insert and increment no. of elements

      }

 Click Here to Download Source Code with Executable Program.

Posted in , , , | Leave a comment

C - Program to Implement Hashing , handle collision using linear probing with chaining and with replacement

C - Program to Implement Hashing , handle collision  using linear probing with chaining and with replacement

 

#include <stdio.h>
#include <conio.h>
#define SIZE 10              /* size of the hash table*/
#define FALSE 0
#define TRUE 1
#define h(x) x%SIZE         /*hashing function */



void insert( int data[],int flag[],int chain[],int x);
int search(int data[],int flag[],int chain[],int x);
void print(int data[],int flag[],int chain[]);


void main()
 {
    int data[SIZE],flag[SIZE],chain[SIZE],i,j,x,op,loc;
    /* array data[]  - is a hash table
       array flag[]  - if flag[i] is 1 then the ith place of the hash
               table is filled */
    for(i=0;i<SIZE;i++) /* initialize */
       {
        flag[i]=FALSE;
        chain[i]=-1;
       }
   clrscr();
    do
      {
        flushall();
        printf("\n\n1)Insert\n2)Search\n3)Print\n4)Quit");
        printf("\nEnter Your Choice : ");
        scanf("%d",&op);
        switch(op)
           {
            case 1: printf("\n Enter a number to be inserted:");
                scanf("%d",&x);
                insert(data,flag,chain,x);
                break;
            case 2: printf("\n Enter a number to be searched :");
                scanf("%d",&x);
                if((loc=search(data,flag,chain,x))==-1)
                    printf("\n****Element not found****");
                else
                    printf("\n***Found at the location=%d",loc);
                break;
            case 3: print(data,flag,chain);
                break;
           }
    }while(op!=4);
  }

void insert( int data[],int flag[],int chain[],int x)
{
    int i=0,j,start;
    start=h(x); /*hashed location*/
    /*Situation I, hashed location is empty*/
    if(flag[start]==0)
      {
        data[start]=x;
        flag[start]=1;
        return;
      }
 /*Situation II, hashed location does not contain a synonym*/
    if(h(data[start])!=h(x))
      { /* locate an empty location */
        i=0;j=start;
        while(flag[j] && i<SIZE)
           {
            j=(j+1)%SIZE;
            i++;
           }
        if(i==SIZE)
           {
            printf("\nTable is full ...");
            return;
           }
    /*Delete the element by modifying the chain */
        i=data[start]%SIZE;/*beginning of the chain*/
        while(chain[i] != start)
            i=chain[i];
        chain[i]=chain[start]; /*modify */
    /*add the deleted element at the end of the chain */
        while(chain[i]!=-1)
            i=chain[i];
        chain[i]=j;
        data[j]=data[start];
        chain[start]=-1;
        flag[j]=1;
        chain[j]=-1;
    /*insert the current key */
        data[start]=x;
        chain[start]=-1;
        return;
      }
    /*Situation III ,hashed location contains a synonym */
    /* locate an empty location */
    i=0;j=start;
    while(flag[j] && i<SIZE)
       {
        j=(j+1)%SIZE;
        i++;
       }
    if(i==SIZE)
       {
        printf("\nTable is full ...");
        return;
       }
    data[j]=x;
    flag[j]=1;
    chain[j]=-1;
    /*go to the end of chain */
    i=start;/*beginning of the chain*/
    while(chain[i] != -1)
        i=chain[i];
    chain[i]=j;
}

int search(int data[],int flag[],int chain[],int x)
 {
    int i=0,j;
    j=h(x); /*hashed location*/
    /*locate beginning of the chain*/
    while(i<SIZE && flag[j] && data[j]%SIZE !=x%SIZE)
       {
        i++;
        j=(j+1)%SIZE;
       }
    if(!flag[j] || i==SIZE)
        return(-1);
    /*locate the element in the chain */
    while(j!=-1)
       {
        if(data[j]==x)
            return(j);
        j=chain[j];
       }
    return(-1);
}

void print(int data[],int flag[],int chain[])
 {
    int i;
    for(i=0;i<SIZE;i++)
        if(flag[i])
            printf("\n(%d) %d     %d",i,data[i],chain[i]);
        else
            printf("\n(%d) ---    %d",i,chain[i]);
 }

 Click Here to Download Source Code with Executable Program


Posted in , | 2 Comments

C - Program for Operations on Polynomial using a circular linked list

 C - Program for Operations on Polynomial using a circular linked list



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

typedef struct  node
 {
    int power;
    float coeff;
    struct node *next;
 }node;

    node *  insert(node *head,int power1,float coeff1);
    node *  create();
    node *  padd(node *p1,node *p2);
    node *  pmul(node *p1,node *p2);
    float eval(node *p1,float x);
    void print(node *head);

node *insert(node *head,int power1,float coeff1)
      {
    node *p,*q;
    /*terms are stored in decreasing  order of power*/
    /*pointer is used to locate the correct place of insertion.
      pointer p is used to store the address of the node created for
     the current term.If a term with same power is found,coefficients
     are added*/
    p=(node*) malloc(sizeof(node));
    p->power=power1;
    p->coeff=coeff1;
    p->next=NULL;
    if(head==NULL) //Empty linked list
       {
        head=p;
        head->next=head;
        return(head);
       }
    if(power1<head->power)//lowest power,inserted at the end
       {
        p->next=head->next;
        head->next=p;
        head=p;
        return(head);
       }
    if(power1==head->power) //add coefficients
       {
        head->coeff=head->coeff+coeff1;
        return(head);
       }

     q=head;
     while(q->next!=head && power1<=q->next->power) //locate the postion for insertion
        q=q->next;
     if(p->power==q->power)
        q->coeff=q->coeff+coeff1;
     else
       {
        p->next=q->next;
        q->next=p;
       }
    return(head);
  }


 node *create()
   {
    int n,i,power1;
    float coeff1;
    node *head=NULL;
    printf("\nEnter No. of Terms:");
    scanf("%d",&n);
    printf("\nenter a term as a tuple of (power,coefficient) : ");
    for(i=1;i<=n;i++)
       {
        scanf("%d%f",&power1,&coeff1);
        head=insert(head,power1,coeff1);
       }
       return(head);
  }

 node * padd(node *p1,node *p2)
 {
    node *p;
    node *head=NULL;
    int power;float coeff;
    p=p1->next;
    do     //insert the first polynomial
       {
        head=insert(head,p->power,p->coeff);
        p=p->next;
       } while(p!=p1->next);
    p=p2->next;
    do  //insert the second polynomial
       {
        head=insert(head,p->power,p->coeff);
        p=p->next;
     } while(p!=p2->next);
    return(head);
  }

 node *pmul(node *p1,node *p2)
 {
    node *head1,*head2;
    node *head=NULL;
    head2=p2->next;
    do   //for every term of the second polynomial
       {
        head1=p1->next;
        do  //multiply with every term of the first polynomial
           {
 //    for(p=head1;p!=NULL;p=p->next)
            head=insert(head,head1->power+head2->power,head1->coeff * head2->coeff);
            head1=head1->next;
            }while(head1!=p1->next);
        head2=head2->next;
       }while(head2!=p2->next);
    return(head);
 }

 float eval(node *head,float x)
 {
    float value=0.00;
    node *p;
    p=head->next;
    do
      {
        value=value+p->coeff * pow(x,p->power);
        p=p->next;
      }while(p!=head->next);
    return(value);
 }

 void print( node *head)
 {
    node *p;
    p=head->next;
    printf("\n");
    do
       {
        printf("%6.2fx^%d   ",p->coeff,p->power);
        p=p->next;
       }while(p!=head->next);
 }


 void main()
 {
    node *p1,*p2,*p3;
    int op;
    float value,x;
    p1=p2=p3=NULL;
    clrscr();
    do
      {
        printf("\n1)Create first polynomial");
        printf("\n2)Create second polynomial");
        printf("\n3)Print first polynomial");
        printf("\n4)Print second polynomial");
        printf("\n5)Add\n6)Multiply\n7)Evaluate First Polynomial\n8)Quit");
        printf("\nEnter Your Choice: ");
        scanf("%d",&op);
        switch(op)
           {
            case 1: p1=create();break;
            case 2: p2=create();break;
            case 3: print(p1);break;
            case 4: print(p2);break;
            case 5: p3=padd(p1,p2);
                print(p3);break;
            case 6: p3=pmul(p1,p2);
                print(p3);break;
            case 7: printf("\nEnter the value of X:");
                scanf("%f",&x);
                value=eval(p1,x);
                printf("\nEvaluated value = %6.2f",value);
                break;
            }
     }while(op!=8);
 }

 Click Here to Download Source Code with Executable Program

Posted in , | Leave a comment

C - Program for Doubly Linked List

C - Program for Implementation Doubly Linked List



/* Accept input as a string and construct a Doubly Linked List for the input
   string with each node contains, as a data one character from the string and
   perform :
   a) Insert b) delete c)Display forward d) Display backward
*/

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

typedef struct node
   { char data;
     struct node *next,*prev;
   }node;

node *create();
node *insert_b(node *head,char x);
node *insert_e(node *head,char x);
node *insert_in(node *head,char x);
node *delete_b(node *head);
node *delete_e(node *head);
node *delete_in(node *head);
void displayforward(node *head);
void displaybackward(node *head);
void main()
{ int op,op1;
  char x;
  node *head=NULL;
  clrscr();
  do
    { flushall();
      printf("\n\n1)create\n2)Insert\n3)Delete");
      printf("\n4)Display forward\n5)Display backward\n6)Quit");
      printf("\nEnter your Choice:");
      scanf("%d",&op);
      switch(op)
       { case 1:head=create();break;
     case 2:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");
        printf("\nEnter your choice : ");
        scanf("%d",&op1);
        printf("\nEnter the data to be inserted : ");
        flushall();
        x=getchar();
        switch(op1)
         {  case 1: head=insert_b(head,x);
                break;
            case 2: head=insert_e(head,x);
                break;
            case 3: head=insert_in(head, x);
                break;
          }
        break;
     case 3:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");
        printf("\nEnter your choice : ");
        scanf("%d",&op1);
        switch(op1)
         {  case 1:head=delete_b(head);
               break;
            case 2:head=delete_e(head);
               break;
            case 3:head=delete_in(head);
               break;
          }
         break;
     case 4:displayforward(head);break;
     case 5:displaybackward(head);break;
       }
    }while(op!=6);
}

node *create()
{ node *head,*p;
  char x;
  head=NULL;
  printf("\n Enter a string :");
  flushall();
  while((x=getchar())!='\n')
   {
     if(head==NULL)
    {
          head=p=(node*)malloc(sizeof(node));
          head->next=head->prev=NULL;
    }
     else
       {
          p->next=(node*)malloc(sizeof(node));
          p->next->prev=p;
          p=p->next;
          p->next=NULL;
       }
    p->data=x;
   }
 return(head);
}

node *insert_b(node *head,char  x)
{   node *p;
    p=(node*)malloc(sizeof(node));
    p->data=x;
   if(head==NULL)
    {
          head=p;
          head->next=head->prev=NULL;
    }
   else
       {
          p->next=head;
          head->prev=p;
          p->prev=NULL;
          head=p;
       }
   return(head);
}
node *insert_e(node *head,char  x)
{   node *p,*q;
    p=(node*)malloc(sizeof(node));
    p->data=x;
   if(head==NULL)
    {
          head=p;
          head->next=head->prev=NULL;
    }
   else
       {
          for(q=head; q->next != NULL ; q=q->next)
          ;
          q->next=p;
          p->prev=q;
          p->next=NULL;

       }
   return(head);
}


node *insert_in(node *head,char x)
{   node *p,*q;
    char  y;
    p=(node*)malloc(sizeof(node));
    p->data=x;
    printf("\Insert after which character ? : ");
    flushall();
    y=getchar();
    //locate the lthe data 'y'
    for(q=head ; q != NULL && q->data != y; q=q->next)
    ;
    if(q->data==y)
      {
        p->next=q->next;
        p->prev=q;
        p->next->prev=p;
        p->prev->next=p;
      }
    else
       printf("\nData not found ");
    return(head);
}
node *delete_b(node *head)
{
  node *p,*q;
  if(head==NULL)
     {
      printf("\nUnderflow....Empty Linked List");
      return(head);
     }
  if(head->next==NULL)//delete the only node
    {
     p=head;
     free(p);
     head=NULL;
    }
  else
    {
     p=head;
     head=head->next;
     head->prev=NULL;
     free(p);
    }
  return(head);

}
node *delete_e(node *head)
{
  node *p;
  if(head==NULL)
     {
      printf("\nUnderflow....Empty Linked List");
      return(head);
     }
  if(head->next==NULL)//delete the only node
    {
      p=head;
      free(p);
      head=NULL;
    }
  else
    {
      for(p=head ; p->next != NULL ; p=p->next)
      ;
      p->prev->next=NULL;
      free(p);
    }
  return(head);

}

node *delete_in(node *head)
{
  node *p,*q;
  char x;
  if(head==NULL)
     {
    printf("\nUnderflow....Empty Linked List");
    return(head);
     }
  printf("\nEnter the data to be deleted : ");
  flushall();
  x=getchar();
  for(p=head ; p !=NULL && p->data != x ; p=p->next)
  ;

   if(p->data!=x)
     {
      printf("\nUnderflow.....data not found");
      return(head);
     }
 if(p==head)
  {
      head=head->next;
      if(head != NULL)
         head->prev=NULL;
      free(p);
  }
 else
  {
      p->prev->next=p->next;
      p->next->prev=p->prev;
      free(p);
   }
 return(head);
}

void displayforward(node *head)
{ node *p;
  printf("\n");
  if(head != NULL)
     {
     for(p=head ;p!=NULL ; p=p->next)
        printf("%c",p->data);
     }
}
void displaybackward(node *head)
{ node *p;
  printf("\n");
  if(head != NULL)
     {   // go to the last node
     for(p=head ;p->next!=NULL ; p=p->next)
     ;
     for( ;p!=NULL ; p=p->prev)
        printf("%c",p->data);
     }
}

Click Here to Download Source Code with Executable Program.


Posted in | Leave a comment