Cod sursa(job #2453646)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 4 septembrie 2019 22:20:06
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
#include <iostream>
#define mod 666013
using namespace std;

ifstream f("hashuri.in");
ofstream g("hashuri.out");

struct nod{
    int val;
    nod *next;
};
nod *lista[mod];

int n;

int hFunc(int x) {
    return x%mod;
}

void add(int x) {
    nod *p = new nod;
    p->val = x;
    p->next = lista[hFunc(x)];
    lista[hFunc(x)] = p;
}

bool query(int x) {
    for(nod *p = lista[hFunc(x)]; p; p = p->next)
        if(p->val == x)
            return true;
    return false;
}

void del(int x) {
    nod *p = lista[hFunc(x)];
    if(!query(x))
        return;
    if(p->val == x) {
        lista[hFunc(x)] = p->next;
        delete p;
        return;
    }
    while(p-> next->val != x)
        p = p->next;
    nod *q = p->next;
    p->next = q->next;
    delete q;
}


int main() {
    f >> n;
    for(int i = 1; i <= n; i++) {
        int op, x;
        f >> op >> x;
        cout << op << '\n';
        if(op == 1)
            add(x);
        else if (op == 2)
            del(x);
        else
            g << query(x) << '\n';
    }
}