Cod sursa(job #2007145)

Utilizator SagunistuStrimbu Alexandru Sagunistu Data 1 august 2017 23:45:27
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.88 kb
#include <fstream>
#define mod 665993

using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

int n;

class Hash
{

private:

    struct node
    {
        int value;
        node *next;
    };

    node* table[mod + 10];

    int hashFunction(int x)
    {
        return x % mod;
    }

    void Find(int x,int &i,node* &p)
    {
        i = hashFunction(x);
        p = table[i];
        while (p != NULL && p->value != x)
            p = p->next;
    }

public:

    void insertElem(int x)
    {
        int i;
        node* p;
        Find(x,i,p);
        if (p == NULL)
        {
            p = new node;
            p->next = table[i];
            p->value = x;
            table[i] = p;
        }
    }

    bool isElem(int x)
    {
        int i;
        node* p;
        Find(x,i,p);
        return (p != NULL);
    }

    void deleteElem(int x)
    {
        int i;
        node* p, *q;
        i = hashFunction(x);
        if(table[i] != NULL)
            if(table[i]->value == x)
            {
                p = table[i];
                table[i] = p->next;
                delete p;
            }
            else
            {
                p = table[i];
                while (p->next != NULL && p->next->value != x)
                    p = p->next;
                if(p->next != NULL)
                {
                    q = p->next;
                    p->next = q->next;
                    delete q;
                }
            }
    }

};

Hash hashura;

int main()
{
    fin >> n;
    int op,x;
    for(int i = 0; i < n;i++)
    {
        fin >> op >> x;
        if(op == 1)
            hashura.insertElem(x);
        else
        if(op == 2)
            hashura.deleteElem(x);
        else
            fout << hashura.isElem(x) << '\n';
    }
    return 0;
}