Cod sursa(job #2233168)

Utilizator Horia14Horia Banciu Horia14 Data 22 august 2018 14:21:48
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include<cstdio>
#define MOD 660013
using namespace std;

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

node* H[MOD];

inline int h(int x) {
    return x % MOD;
}

bool searchKey(int x) {
    node* p = H[h(x)];
    while(p != NULL && p->val != x)
        p = p->next;
    if(p != NULL)
        return true;
    return false;
}

inline void insertKey(int x) {
    int l = h(x);
    node* elem = new node;
    elem->val = x;
    elem->next = H[l];
    H[l] = elem;
}

void deleteKey(int x) {
    node* p, *q;
    int l = h(x);
    if(H[l]->val == x)
        H[l] = H[l]->next;
    else {
        p = H[l];
        while(p->next->val != x)
            p = p->next;
        q = p->next;
        p->next = q->next;
        delete q;
    }
}

int main() {
    int op, x, i, n;
    FILE* fin, *fout;
    fin = fopen("hashuri.in","r");
    fout = fopen("hashuri.out","w");
    fscanf(fin,"%d",&n);
    for(i = 1; i <= n; i++) {
        fscanf(fin,"%d%d",&op,&x);
        if(op == 1) {
            if(!searchKey(x))
                insertKey(x);
        } else if(op == 2) {
            if(searchKey(x))
                deleteKey(x);
        } else fprintf(fout,"%d\n",searchKey(x));
    }
    fclose(fin);
    fclose(fout);
    return 0;
}