Cod sursa(job #734975)

Utilizator yamahaFMI Maria Stoica yamaha Data 15 aprilie 2012 13:12:56
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 3.31 kb
#include <fstream>

using namespace std;

template <class T>
class node
{
    public:

    T key;
    node *next;
};

template <class T>
class table
{
    node<T> *v;
    int size_of_hash;

    public:
        table<T>();

        void insert(T);
        void erase(T);
        bool empty();
        bool find(T);
        int size();
        node<T>* first();
        node<T>* last();
};

template <class T>
table<T>::table()
{
    v = NULL;
    size_of_hash = 0;
}

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

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

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

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

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

template <class T>
void table<T>::insert(T x)
{
    node<T> *p,*newnode;
    if (find(x))
        return;

    newnode = new node<T>;
    newnode->key = x;
    newnode->next = NULL;

    if (empty())
    {
        v = newnode;
    }
    else
    {
        p = last();
        p->next = newnode;
    }

    size_of_hash++;
}

template <class T>
void table<T>::erase(T x)
{
    node<T> *p, *u;
    if (!find(x))
        return;

    if (size_of_hash == 1)
    {
        v = NULL;
    }
    else
    {
        u = first();
        if (u->key == x)
            v = v->next;
        else
        {
            p = u->next;
            while (p != NULL)
            {
                if (p->key == x)
                {
                    u->next = p->next;
                    delete p;
                    break;
                }
                u = p;
                p = p->next;
            }
        }
    }

    size_of_hash--;
}

template <class T>
class hash
{
    table<T> *vector;
    int prim;

    public:
        hash<T>();

        bool insert(T);
        bool erase(T);
        short int search(T);
        //int h(T x) { int index = x % prim; return index; }

};

template <class T>
hash<T>::hash()
{
    prim = 640013;
    vector = new table<T>[prim];
}

template <class T>
bool hash<T>::insert(T x)
{
    int index = x%prim;

    vector[index].insert(x);
    return true;
}

template <class T>
bool hash<T>::erase(T x)
{
    int index = x%prim;

    vector[index].erase(x);
    return true;
}

template <class T>
short int hash<T>::search(T x)
{
    int index = x%prim;

    if (vector[index].find(x))
        return 1;
    return 0;
}


int main()
{
    int n,op,x;
    ifstream f("hashuri.in");
    ofstream g("hashuri.out");

    hash<int> H;
    f>>n;
    for (; n; --n)
    {
        f>>op>>x;
        switch (op)
        {
            case 1:
                H.insert(x);
                break;
            case 2:
                H.erase(x);
                break;
            case 3:
                g<<H.search(x)<<endl;
                break;
        }
    }

    return 0;
}