Cod sursa(job #1708046)

Utilizator dennsDance Denis denns Data 26 mai 2016 14:19:01
Problema Hashuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 3.6 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
template <class T1, class T2>
class Entry
{
    T1 key;
    T2 value;
    Entry *next;

public:
    Entry(T1 x,T2 y)
    {
        this->key=x;
        value=y;
        next=NULL;

    }
    T2 get_value()
    {
        return value;
    }
    T1 get_key()
    {
        return this->key;
    }
    Entry * get_next()
    {
        return next;
    }
    void set_next(Entry *x)
    {
        next=x;
    }

};
template <class T1, class T2>
class Hash {
    Entry<T1,T2> **a;
    int length;
    int a_size;
    int (*hash_f)(T1,int);
public:
    void init(int x)
    {
        a_size=x;
          a=new Entry<T1,T2>*[a_size];
        for(int i=0;i<a_size;i++)
            a[i]=NULL;
    }
    Hash(int x,int (*f)(T1,int))
    {
        hash_f=f;
        length=0;
        init(x);
    }

    void put(T1 key,T2 value)
    {
       {
            int pos=key%a_size;
            Entry <T1,T2>*q;
            q=new Entry<T1,T2>(key,value);
            q->set_next(a[pos]);
            a[pos]=q;
            length ++;
        }
         if(length>a_size * 50)
            this->realloc();
    }
    T2 operator [](T1 key)
    {

        int pos=key%a_size;
        while ( a[pos]!=NULL && a[pos]->get_key()!=key)
           a[pos]=a[pos]->get_next();
        if(a[pos]!=NULL)
            return a[pos]->get_value();
        return NULL;
    }
    Hash& operator= (Hash & x)
    {
         delete[] a;
        this->init(x.a_size);
        this->length=x.length;
        for(int i=0;i<x.a_size;i++)
            if(x.a[i])
            this->put(x.a[i]->get_key(),x.a[i]->get_value());
        return *this;
    }
    ~Hash()
    {
        delete[] a;
    }
    void realloc()
    {
        Hash temp(a_size*2,hash_f);
        for(int i=0;i<a_size;i++)
            if(this->a[i])
                temp.put(a[i]->get_key(),a[i]->get_value());
        *this=temp;
    }
    void del(T1 key)
    {
        Entry<T1,T2> *t,*p;
        int pos=key%a_size;
       if(a[pos]->get_key()==key)
            a[pos]=NULL;
       else
       {
           p=a[pos];
           while( p!=NULL && p->get_key()!=key)
           {
               t=p;
               p=p->get_next();
           }

            t->set_next(p->get_next());
       }
    }
    int exists(T1 key)
    {
        Entry<T1,T2> *p;
       // cout<<endl<<key%a_size;
       // int pos=this->hash_f(key,a_size);
        int pos=key%a_size;
        //cout<<pos<<" ";

        p=a[pos];
        while ( p!=NULL && p->get_key()!=key)
           p=p->get_next() ;
           if(p && p->get_key()==key)
                return 1;
           return 0;
    }
    void afisare()
    {
        Entry<T1,T2> *p;
        for(int i=0;i<a_size;i++)
            if(this->a[i])
            {
                for(p=a[i];p!=NULL;p=p->get_next())

                  cout<<p->get_key()<<" "<<p->get_value()<<endl;

            }

    }

};

  int hashing (int x,int size_ar)
  {
      return x%size_ar;
  }
int main()
{
    int n,op,x;
    Hash<int,int> mp(3,&hashing);
    f>>n;
    for(int i=0;i<n;i++)
    {
        f>>op>>x;
        switch(op)
        {
        case 1:
         //cout<<x<<endl;
            if(!mp.exists(x))
                mp.put(x,1);
            break;
        case 2:
            if(mp.exists(x))
            mp.del(x);
            break;
        case 3:
            g<<mp.exists(x)<<endl;
            break;
        }
    }
  return 0;
}