Pagini recente » Cod sursa (job #2309828) | Cod sursa (job #236708) | Cod sursa (job #1221483) | Cod sursa (job #2458593) | Cod sursa (job #1562991)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
const int M = 3999971;
struct nod
{
int val;
nod *next;
nod(int v=0) { this->val = v; this->next = NULL; }
};
nod* hashMap[M];
int getHash(int key)
{
return key%M;
}
void addKey(int key)
{
nod *x = new nod(key);
x->next = hashMap[getHash(key)];
hashMap[getHash(key)] = x;
}
nod* get(int key)
{
for (nod *crt = hashMap[getHash(key)]; crt != NULL; crt = crt->next)
if (crt->val == key)
return crt;
return NULL;
}
void removeKey(int key)
{
nod **pls;
pls = &hashMap[getHash(key)];
for (nod *crt = hashMap[getHash(key)]; crt != NULL; pls = &(crt->next), crt = crt->next)
if (crt->val == key) {
*pls = crt->next;
delete crt;
}
}
int main()
{
int n, op, key;
in>>n;
for (int i = 0; i < n; i++) {
in>>op>>key;
switch (op) {
case 1: addKey(key); break;
case 2: removeKey(key); break;
default: out<<get(key) != NULL;
}
}
return 0;
}