Cod sursa(job #1796380)

Utilizator andreiulianAndrei andreiulian Data 3 noiembrie 2016 14:16:42
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include<iostream>
#include<fstream>
#define NN 13831
using namespace std;
void adauga(int x);
bool gaseste(int x);
void sterge(int x);
struct nod {
    int val;
    nod *next;
};
 
nod *h[NN];
int main(){
    freopen ("hashuri.in", "r", stdin);
    freopen ("hashuri.out", "w", stdout);
    int n, x, a;
    cin >> n;
    while (n--){
        cin >> a >> x;
        switch (a) {
            case 1: adauga(x); break;
            case 2: sterge(x); break;
            case 3: cout << gaseste(x) << '\n'; break;
        }
    }
}
void sterge(int x) {
    int v = x % NN;
    nod *q = h[v];
    if (q == NULL) return;
    if (q -> val == x) {
        h[v] = q -> next, delete(q);
    } else {
        nod *qq = q -> next;
        while(qq != NULL && qq -> val != x) {
            q = qq;
            qq = qq -> next;
        }
        if (qq != NULL) {
            q -> next = qq -> next;
            delete(qq);
        }
    }
}
 
bool gaseste(int x) {
    int v = x % NN;
    nod *q = h[v];
    while (q != NULL && q -> val != x) {
        q = q -> next;
    }
    if (q != NULL) return 1;
    return 0;
}
 
void adauga(int x) {
    int v = x % NN;
    if (h[v] == NULL) {
        h[v] = new nod;
        h[v] -> next = NULL;
        h[v] -> val = x;
    } else {
        nod *q = new nod;
        q -> val = x;
        q -> next = h[v];
        h[v] = q;
    }
}