Cod sursa(job #734182)

Utilizator yamahaFMI Maria Stoica yamaha Data 13 aprilie 2012 19:40:05
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.58 kb
#include <fstream>
#include <cstdlib>
using namespace std;

//#define max 1000003
#define prim 670013

template <class T> // asa arata un nod din lista
class node
{
    public:
           T key;
           node * next;
};

template <class T> // tabelul hash - stl implementat de mana
class table
{
    node<T> *v;
    int table_size;
    
    public:
           table() { v = NULL; table_size = 0; }
           int size();
           bool empty();
           node<T> *first();
           node<T> *last();
           bool search(T);
           void insert(T);
           void erase(T);        
};

template <class T>
int table<T> :: size()
{
    return table_size;
}

template <class T>
bool table<T> :: empty()
{
    if(table_size) return false;
    return true;
}

template <class T>
node<T> *table<T> :: first()
{
    return v;
}

template <class T>
node<T> *table<T> :: last()
{
    node<T> *p = v;
    while(p->next) p = p->next;
    return p;
}

template <class T>
bool table<T> :: search(T x)
{
    node<T> *p;
    if(empty()) return false;
    p = first();
    while(p)
    {
         if(p->key == x) return true;
         p = p->next;
    }
    return false;
}

template <class T>
void table<T> :: insert(T x)
{
    node<T> *p, *q;
    if(search(x)) return;
    
    q = new node<T>;
    q->key = x;
    q->next = NULL;
    
    if(empty()) v = q;
    else { p = last(); p->next = q; }
    
    table_size++;
}

template <class T>
void table<T> :: erase(T x)
{
    node<T> *p, *q;
    if(!search(x)) return;
    
    // daca exista in tabel
    if(table_size == 1) v = NULL;
    else
    {
         // daca e pe prima pozitie
         q = first();
         if(q->key == x) q = q->next;
         else // daca nu
         {
              p = q->next;
              while(p)
              {
                   if(p->key == x)
                   {
                        q->next = p->next;
                        delete p;
                        break;
                   }
                   q = p;
                   p = p->next;
              }
         }
    }
    
    table_size--;
}


template <class T>
class hash
{
    table<T> *H;
    //int a, b;
    
    public:
           hash() { H = new table<T> [prim]; }
           //void random();
           int h(T);
           bool search(T);
           bool insert(T);
           bool erase(T);
};
/*
template <class T>
void hash<T> :: random()
{
    a = rand()%40;
    b = rand()%40 + 1;
}*/

template <class T>
int hash<T> :: h(T x)
{
    /*int index = a*x+b;
    index %= prim;
    index %= max;
    return index;
    */
    int index = x%prim;
    return index;
}

template <class T>
bool hash<T> :: search(T x)
{
    int index = h(x);
    if(H[index].search(x)) return 1;
    return 0;
}

template <class T>
bool hash<T> :: insert(T x)
{
    int index = h(x);
    H[index].insert(x);
    return true;
}

template <class T>
bool hash<T> :: erase(T x)
{
    int index = h(x);
    H[index].erase(x);
    return true;
}


int main ()
{
    ifstream f("grader_test5.in");
    ofstream g("hashuri.out");
    
    hash <int> hsh;
    //hsh.random();
    
    int N, i, t, x;
    f>>N;
    for(i=1;i<=N;++i)
    {
         f>>t>>x; 
         if(t == 1)
              hsh.insert(x);
         else if(t == 2)
              hsh.erase(x);
         else if(t == 3)
         { 
              g<<hsh.search(x)<<endl;//<<" - "<<x<<endl;
         }
    }
    f.close(); g.close();
    
    
    return 0;
}