Cod sursa(job #283155)

Utilizator conttPop Mircea contt Data 18 martie 2009 20:09:13
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 2.08 kb
#include <iostream>
#define p 666013

using namespace std;

struct NOD
{
    int val;
    struct NOD *next;
};

void add (long);
void remove (long);
void search (long);

NOD* list[p];
int n;

int main()
{
   long i, op, x;
 
    freopen ("hashuri.in", "r", stdin);
    freopen ("hashuri.out", "w", stdout);
    
    cin >> n;
    for (i = 1; i <= n; i++)
    {
        cin >> op >> x;
        if (op == 1)
        {
            add (x);
        }
        else if (op == 2)
        {
            remove (x);
        }
        else
        {
            search (x);
        }
    }
    return 0;
}

void search (long x)
{
    NOD *t;
    t = list[x%p];
    while (t)
    {
        if (t->val == x)
        {
            cout << "1\n";
            return;
        }
        t = t->next;
    }
    cout << "0\n";
}

 void remove (long x)
 {
      long indice=x%p;
    NOD *t=list[indice], *u;
    if (!t)
    {
        return;
    }
    else
    {
        //t = list[x%p];
        if (t->val == x)
        {
            list[indice] = t->next;
            delete t;
        }
        else
        {
            u = t;
            while (1)
            {
                if (!t->next) return;
                t = t->next;
                if (t->val == x)
                {
                    u->next = t->next;
                    delete t;
                    return;
                }
                u = t;
            }
        }
    }
}

 void add (long x)
{long indice=x%p;
    NOD *t=list[indice];
    if (!t)
    {
        t=list[indice] = new NOD;
       // t = list[indice];
        t->val = x;
        t->next = 0;
    }
    else
    {
       // t = list[x%p];
        while (1)
        {
            if (t->val == x)
            {
                return;
            }
            else
            {
                if (t->next)
                {
                    t = t->next;
                }
                else
                {
                    t->next = new NOD;
                    t = t->next;
                    t->val = x;
                    t->next = NULL;
                }
            }
        }
    }
}