Sunday, 16 October 2011

OPEN HASHING


PROGRAM:
//to perform hashing by seperate chaining
#include<stdio.h>    //including header file
struct tree              //definition for structure tree
{
          int data;                 //data field
          struct tree *next;    //reference field
}*p,*new;           //object for structure
int main()
{
          system("clear");
          int a[10],i,x,c[10],t,z,ptr;
          for(i=0;i<10;i++)
          {
                   a[i]=0;           //initializing array to zero
          }
          do
          {
                   printf("\n enter the data");
                   scanf("%d",&x);   //getting element from user
                   new=(struct tree*)malloc(sizeof(struct tree));  //dynamic memory  allocation
                   new->data=x;  //assigning element to data field
                   new->next=NULL;
                   t=x%10;
                   if(a[t]==0)
                   {
                             p=new;
                             a[t]=p;
                   }
                   else
                   {
                             p=a[t];
                             new->next=p;
          //                 p->next=NULL;
                             a[t]=new;
                            
                   }
                   printf("\n press 1 to continue");   //getting choice from user
                   scanf("%d",&z);
          }while(z==1);
          for(i=0;i<10;i++)
          {
                   if(a[i]==0)
                   {
                             printf("%d \n",a[i]);   //printing the elements
                   }
                   else
                   {
                             p=a[i];
                             while(p!=NULL)
                             {
                                      printf(" %d \t",p->data);
                                      p=p->next;      //moving to next node
                             }
                             printf("\n");
                   }
          }
}























OUTPUT:
 enter the data1

 press 1 to continue1

 enter the data2

 press 1 to continue1

 enter the data11

 press 1 to continue3
0
 11      1
 2
0
0
0
0
0
0

No comments:

Post a Comment