Cod sursa(job #2376815)

Utilizator Horia14Horia Banciu Horia14 Data 8 martie 2019 17:53:26
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include<cstdio>
#define MOD 666013
using namespace std;

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

node* g[MOD];

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

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

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

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

int main() {
    int op, x, n, i;
    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;
}