Pagini recente » Cod sursa (job #2411018) | Cod sursa (job #905574) | bon | Cod sursa (job #425308) | Cod sursa (job #2739382)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
const int prime = 666013;
struct list {
list* next=NULL;
long long val=-1;
};
list* hashh[prime];
int getHashed(long long x) {
return x % prime;
}
void insert(long long x) {
int hashed = getHashed(x);
if (hashh[hashed] == NULL) {
hashh[hashed] = new list;
hashh[hashed]->next = NULL;
hashh[hashed]->val = x;
}
else {
list* curr = hashh[hashed];
bool OK=1;
while (curr->next != NULL) {
if (curr->val == x) {
OK = 0;
break;
}
curr = curr->next;
}
if (curr->val == x)
OK = 0;
if (OK == 1) {
list* aux= new list;
aux->val = x;
curr->next = aux;
}
}
}
void erase(long long x) {
int hashed = getHashed(x);
if (hashh[hashed] != NULL) {
if (hashh[hashed]->val == x) {
hashh[hashed] = hashh[hashed]->next;
}
else if(hashh[hashed]->next!=NULL){
list* curr = hashh[hashed];
while (curr->next->next != NULL) {
if (curr->next->val == x) {
curr->next = curr->next->next;
break;
}
curr = curr->next;
}
if (curr->next->val == x)
curr->next = NULL;
}
}
}
bool exists(long long x) {
int hashed = getHashed(x);
list* curr = hashh[hashed];
while (curr != NULL) {
if (curr->val == x)
return 1;
curr = curr->next;
}
return 0;
}
int main()
{
int n,opt;
long long x;
f >> n;
for (int i = 0; i < n; i++) {
f >> opt >> x;
switch (opt) {
case 1:
insert(x);
break;
case 2:
erase(x);
break;
case 3:
if (exists(x))
g << 1<<"\n";
else g << 0<<"\n";
break;
}
}
}