Monday 24 October 2011

Program on Hashing Chain



#include<iostream>
#include<cstdlib>
using namespace std;
class hashch
{
      struct node
      {
             int value;
             node *link;
      };
     public:
            struct node *hashtable[10];
            void insert(int);
            void del(int);
            bool search(int);
            void display();
            void inp();

};
void hashch::inp()
{
 for(int i=0;i<10;i++)
     {
                      hashtable[i]=NULL;
     }
}
void hashch::insert(int val)
{
     int c,i;

     node *t;
     t=new node;
     t->value=val;
     t->link=NULL;
     c=val%10;
     if(hashtable[c]==NULL)
           hashtable[c]=t;
     else
     {
         node*p;
         p=new node;
         p=hashtable[c];
         if(val<p->value)
         {
                       t->link=p;
                       hashtable[c]=t;
         }
         else
         {
             while (val>p->link->value&&p->link!=NULL)
             p=p->link;
             if(p->link->link!=NULL)
            {
                t->link=p->link;
                p->link=t;
             }
             else if(val<p->link->value)
             {
                  t->link=p->link;
                  p->link=t;
             }
             else
             p->link->link=t;
         }
     }
}
void hashch::del(int val)
{
     int c;
     node *t,*p ;
     c=val%10;
     t=new node;
     p=new node;

     if(hashtable[c]!=NULL)
     {
                          if(t->value==val)
                          {
                            t=hashtable[c];
                            hashtable[c]=hashtable[c]->link;
                           delete t;
                          }
                          else
                          {
                                  p=hashtable[c];
                                  while(p->link->link!=NULL&&p->link->value!=val)
                                  {
                                       p=p->link;

                                  }
                                  if(t->link->link!=NULL)
                                  {
                                                          t=p->link;
                                                          p->link=p->link->link;
                                                          free(t);
                                  }
                                   else
                                   t->link=NULL;
                          }
}
                          else
                          cout<<"\nThe bucket doesnt exist\n";
}

bool hashch::search(int val)
{
     int c;
     node *t;
     c=val%10;
     t=hashtable[c];
     if(hashtable[c]!=NULL)
     {
                           while(t!=NULL)
                           {
                                         if(t->value==val)
                                         {
                                                          cout<<"Found\n";
                                                          return true;
                                         }
                                         else
                                         {
                                             t=t->link;
                                         }
                           }


                               cout<<"The value doesnt exist\n";
                                return false;

     }
     else
     cout<<"The bucket is empty\n";
}
void hashch::display()
{
    node *p[10];
   node *t;

     int i;
    for(i=0;i<10;i++)
{
     p[i]=hashtable[i];
}
    for(i=0;i<10;i++)
    {
             t=new node;
                     if(p[i]!=NULL)
                     {
                                   t=p[i];
                                    while(t!=NULL)
                                   {

                                   cout<<t->value<<"\t";
                                    t=t->link;
                                    }
                     cout<<"\n";
                     }
                     else
                     cout<<"NULL\n";
      }
}
int main()
{
    hashch ch;
    int option;
    int ele;

    bool ans;
    ch.inp();
    while(1)
    {
            cout<<"\nEnter your choice\n";
            cout<<"\n1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n";
            cin>>option;
            switch(option)
            {
                          case 1:cout<<"\nEnter what you want to insert\n";
                          cin>>ele;
                          ch.insert(ele);
                               break;
                          case 2:cout<<"\nEnter what you want to delete\n";
                               cin>>ele;
                               ch.del(ele);
                                break;
                          case 3:cout<<"\nEnter what you want to search\n";
                                 cin>>ele;
                                 ans=ch.search(ele);
                                 if(ans)
                                  cout<<"\nFound\n";
                                 else
                                  cout<<"\nMissing\n";
                                 break;
                          case 4: ch.display();
                                  break ;
                          case 5: exit(1);
            }
    }
    return 0;

}




OUTPUT:-

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
1

Enter what you want to insert
2

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
3

Enter what you want to search
2
Found

Found

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
1

Enter what you want to insert
4

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
4
NULL
NULL
2
NULL
4
NULL
NULL
NULL
NULL
NULL

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
1

Enter what you want to insert
6

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
4
NULL
NULL
2
NULL
4
NULL
6
NULL
NULL
NULL

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit

No comments:

Post a Comment