Cod sursa(job #799701)

Utilizator Theorytheo .c Theory Data 19 octombrie 2012 20:58:10
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.69 kb
#include<fstream>

#define M 999997

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

int N;

struct nod{int inf; nod *next; };
nod *v[M + 1];

int H(int x)
{
    return x % M;
}
void add(int x)
{
    int l = H(x);
    while(v[l] -> next != NULL)
        v[l] = v[l] -> next;

    nod *q = new nod;
    q -> next =NULL;
    q -> inf = x;
    v[l] -> next = q;

}
void sterge(int x)
{
    int  l = H(x);
    while(v[l] -> next != NULL && v[l] ->next -> inf != x)
    {
        v[l] = v[l] -> next;
    }


    if(v[l] -> next)
    {
        nod *q = new nod;
        q = v[l] -> next;
        if(v[l] -> next -> next != NULL)
            v[l] -> next = v[l] -> next -> next;
        else
            v[l] -> next = NULL;

        delete (q);
    }
    if(v[l] -> inf == x)
    {
        v[l] -> next = NULL;
        v[l] -> inf= 0;
    }

}
bool verifica(int x)
{
    int l = H(x);

    while(v[l] -> next != NULL)
    {

        if(v[l] -> inf == x)
            return true;
        v[l] = v[l] -> next;
    }
    if(v[l] -> inf == x)
        return true;
    return false;

}
void initializare()
{
    for(int i = 1; i <= M ;i++)
    {
        v[i] = new nod;
        v[i] -> inf = 0;
        v[i] ->  next = NULL;
    }
}
int main()
{
    fin >> N;
    int op, x;
    initializare();

    for(int i = 1; i <= N; i++)
    {
        fin >> op >> x;

            if(op == 1)
                add(x);

            if(op == 2)
                sterge(x);

            if(op == 3)
            {

               fout << verifica(x) <<'\n';

            }


    }
    fin.close();
    return 0;
}