Pagini recente » Cod sursa (job #794008) | Cod sursa (job #1178693) | Cod sursa (job #1207930) | Cod sursa (job #1008447) | Cod sursa (job #1716964)
#include <bits/stdc++.h>
using namespace std;
class LinkedList {
private:
struct Node {
Node* next;
int info;
Node() {}
};
public:
LinkedList() {
pre = new Node();
post = new Node();
pre->next = post;
post->next = nullptr;
}
~LinkedList() {
Node* current = pre;
while (current != nullptr) {
Node* aux = current;
current = current->next;
delete aux;
}
}
LinkedList::Node* find(int value) {
Node* prev = pre;
Node* current = pre->next;
while (current != post) {
if (current->info == value) {
return prev;
}
prev = current;
current = current->next;
}
return nullptr;
}
void append(int value) {
Node* current = find(value);
if (current == nullptr) {
current = new Node();
current->info = value;
Node* aux = pre->next;
pre->next = current;
current->next = aux;
}
}
void remove(int value) {
Node* prev = find(value);
if (prev != nullptr) {
Node* current = prev->next;
prev->next = current->next;
delete current;
}
}
private:
friend class HashTable;
Node* pre;
Node* post;
};
class HashTable {
public:
HashTable() {
for (int i = 0; i < MOD; i++) {
hash[i] = new LinkedList();
}
}
~HashTable() {
for (int i = 0; i < MOD; i++) {
delete hash[i];
}
}
static int hash_function(int value) {
return value % MOD;
}
void add(int value) {
int key = hash_function(value);
hash[key]->append(value);
}
void remove(int value) {
int key = hash_function(value);
hash[key]->remove(value);
}
bool contains(int value) {
int key = hash_function(value);
LinkedList::Node* node = hash[key]->find(value);
return node != nullptr;
}
private:
static const int MOD = 100003;
LinkedList* hash[MOD];
};
int main() {
cin.sync_with_stdio(false);
freopen("hashuri.in", "r", stdin);
freopen("hashuri.out", "w", stdout);
HashTable my_hash;
int n;
cin >> n;
for (; n; n--) {
int op, value;
cin >> op >> value;
if (op == 1) {
my_hash.add(value);
} else if (op == 2) {
my_hash.remove(value);
} else {
cout << my_hash.contains(value) << '\n';
}
}
return 0;
}