Pagini recente » Cod sursa (job #1365647) | Cod sursa (job #2096673) | Cod sursa (job #2814686) | Cod sursa (job #959338) | Cod sursa (job #2165158)
#include<cstdio>
#define MOD 660013
using namespace std;
struct node {
int val;
node* next;
};
node* Hash[MOD];
inline int h(int x) {
return x % MOD;
}
inline bool searchKey(int x) {
int k = h(x);
node* p = Hash[k];
while(p != NULL && p->val != x)
p = p->next;
if(p != NULL)
return 1;
return 0;
}
inline void insertKey(int x) {
int k = h(x);
node* p = new node;
p->val = x;
p->next = Hash[k];
Hash[k] = p;
}
inline void deleteKey(int x) {
int k = h(x);
node *p, *q;
p = Hash[k];
if(p->val == x) {
Hash[k] = Hash[k]->next;
delete p;
} else {
while(p->next->val != x)
p = p->next;
q = p->next;
p->next = p->next->next;
delete q;
}
}
int main() {
int op, x, n;
FILE *fin, *fout;
fin = fopen("hashuri.in","r");
fout = fopen("hashuri.out","w");
fscanf(fin,"%d",&n);
for(int i = 0; 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;
}