Cod sursa(job #1037879)

Utilizator dan.ghitaDan Ghita dan.ghita Data 20 noiembrie 2013 20:26:25
Problema Hashuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
int n, x;
int t;

struct nod{
int val;
nod *next;
};
nod *v[64950];

int hash_f(int a){
return a%64919   ;
}

void adauga(int a){
int h=hash_f(a);
nod *q=new nod;
if(v[h]==NULL) v[h]=new nod, v[h]->next=NULL;
q=v[h];
while(q->next!=NULL){
q=q->next;
}

nod *p=new nod;
p->val=a;
p->next=NULL;
q->next=p;
}

void sterge(int a){
int h=hash_f(a);
nod *q=new nod;
if(v[h]==NULL) return;
q=v[h];

while(q!=NULL){
    nod *p=new nod;
    p=q->next;
    if(p->val==a){
        q->next=p->next;
        delete p;
        return;
    }

    else q=q->next;
}

}

void exista(int a){
int h=hash_f(a);
nod *q=new nod;
if(v[h]==NULL){
        g<<0<<'\n';
        return;
    }
q=v[h];
while(q!=NULL){
    if(q->val==a){
        g<<1<<'\n';
        return;
    }

    else q=q->next;
}

g<<0<<'\n';
return;

}

int main()
{
    f>>n;
    while(n--){
        f>>t>>x;
        if(t==1) adauga(x);
        if(t==2) sterge(x);
        if(t==3) exista(x);
    }




    return 0;
}