Pagini recente » Rezultatele filtrării | Cod sursa (job #3124390) | Minim | Rezultatele filtrării | Cod sursa (job #2781143)
#include <fstream>
#include <list>
using namespace std;
struct Hash {
const size_t hashTableSize = 8e5;
list<long> *array;
Hash() : array(new list<long>[hashTableSize]) {}
~Hash() {
delete[] array;
}
size_t hashIdx(const long &value) {
return value % hashTableSize;
}
void add(const long &value) {
auto id = hashIdx(value);
auto &linkedList = array[id];
for (auto it = linkedList.begin(); it != linkedList.end(); it++)
if (*it == value)
return;
linkedList.push_back(value);
}
void erase(const long &value) {
auto id = hashIdx(value);
auto &linkedList = array[id];
linkedList.remove(value);
}
bool contains(const long &value) {
auto id = hashIdx(value);
auto &linkedList = array[id];
for (auto it = linkedList.begin(); it != linkedList.end(); it++)
if (*it == value)
return true;
return false;
}
};
int main(void) {
ifstream in("hashuri.in");
ofstream out("hashuri.out");
size_t n, i, code, x;
in >> n;
Hash container{};
for (i = 0; i < n; i++) {
in >> code >> x;
switch (code) {
case 1:
container.add(x);
break;
case 2:
container.erase(x);
break;
case 3:
out << container.contains(x) << '\n';
break;
default:
break;
}
}
in.close();
out.close();
return 0;
}