Cod sursa(job #2118230)

Utilizator omegasFilip Ion omegas Data 30 ianuarie 2018 13:20:37
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <fstream>
#define prime 93187
using namespace std;

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

int hsh(int n)
{
    return n%prime;
}

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

nod* T[100000];

void insereaza(int n)
{
    int h = hsh(n);
    nod* p = new nod;
    p->val = n;
    p->next = T[hsh(n)];
    T[hsh(n)] = p;
}

int cauta(int n)
{
    int h = hsh(n);
    nod* p;
    p = T[hsh(n)];
    while(p!=NULL){
        if(p->val == n)
            return 1;
        p = p->next;
    }
    return 0;
}

int sterge(int n)
{
    int h = hsh(n);
    nod* p;
    nod* prev;
    p = T[hsh(n)];
    while(p!=NULL){
        if(p->val == n){
            if(p == T[hsh(n)])
                T[hsh(n)] = p->next;
            else
                prev->next = p->next;
            return 1;
        }
        prev = p;
        p = p->next;
    }
    return 0;
}

int main()
{
    int n,x,y,i;
    fin >> n ;
    for(i=1;i<=n;++i){
        fin >> x >> y;
    if(x == 1)
        insereaza(y);
    else if(x == 2)
        sterge(y);
    else
        fout << cauta(y) << "\n";
    }

    return 0;
}