Pagini recente » Cod sursa (job #2357930) | Cod sursa (job #2484208) | Cod sursa (job #1592361) | Cod sursa (job #2164799) | Cod sursa (job #1200475)
#include <fstream>
#include <cstring>
using namespace std;
const int hashSize = 666013;
struct node{
int info;
node* next;
node(int i) {
info = i;
next = NULL;
}
};
int h(int val) {
return val % hashSize;
}
int find(node* hash[], int val) {
node* root = hash[h(val)];
while(root) {
if (root->info == val) return 1;
root = root->next;
}
return 0;
}
int insert(node* hash[], int val) {
if (find(hash, val)) {
return 0;
}
node* nod = new node(val);
nod->next = hash[h(val)];
hash[h(val)] = nod;
return 1;
}
int remove(node* hash[], int val) {
node* root = hash[h(val)];
if (!root) return 0;
if (root->info == val) {
hash[h(val)] = hash[h(val)]->next;
delete root;
return 1;
}
node* p1 = root->next;
node* prev = root;
while (p1) {
if (p1->info == val) {
prev->next = p1->next;
return 1;
}
p1 = p1->next;
prev = prev->next;
}
return 0;
}
void clearNode(node* root) {
if (root == NULL) return;
clearNode(root->next);
delete root;
}
void clear(node* hash[]) {
for (int i = 0; i < hashSize; i++) {
if (!hash[i]) continue;
clearNode(hash[i]);
}
delete hash;
}
int main() {
ifstream cin("hashuri.in");
ofstream cout("hashuri.out");
node** hash = (node**)malloc(hashSize * sizeof(node*));
memset(hash, NULL, hashSize * sizeof(node*));
int N;
cin >> N;
for (int i = 0; i < N; i++) {
int op, x;
cin >> op >> x;
switch(op) {
case 1:
insert(hash, x);
break;
case 2:
remove(hash, x);
break;
default:
cout << find(hash, x) << '\n';
}
}
clear(hash);
return 0;
}