Pagini recente » Cod sursa (job #2806234) | Cod sursa (job #2965753) | Cod sursa (job #2270236) | Cod sursa (job #3123700) | Cod sursa (job #2637917)
// HashTable.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
template <class T>
class Utility {
public:
static unsigned int hash(const T& t) {
return t;
}
static bool equal(const T& t1, const T& t2) {
return (t1 == t2);
}
};
template <class K, class V, unsigned int(*hash_func)(const K&) = Utility<K>::hash, bool (*equal)(const K&, const K&) = Utility<K>::equal >
class HashTable
{
using uint = unsigned int;
public:
HashTable() {
table = new list[size];
}
bool exists(const K& key) {
uint index = hash_func(key) % size;
node* e = table[index].begin;
while (e != nullptr && equal(key, e->key) == false)
e = e->next;
return (e != nullptr);
}
V* find(const K& key) {
uint index = hash_func(key) % size;
node* e = table[index].begin;
while (e != nullptr && equal(key, e->key) == false)
e = e->next;
if (e == nullptr)
return nullptr;
return &e->value;
}
void push(const K& key, const V& value) {
V* v = find(key);
if (v != nullptr) {
*v = value;
return;
}
uint index = hash_func(key) % size;
node* e = new node;
e->key = key;
e->value = value;
e->next = nullptr;
list& L = table[index];
if (L.begin == nullptr)
L.begin = e;
else
L.end->next = e;
L.end = e;
}
void pop(const K& key) {
uint index = hash_func(key) % size;
node* e = table[index].begin;
if (e == nullptr) return;
while (e->next != nullptr && equal(e->next->key, key) == false) e = e->next;
if (e == table[index].begin) {
delete e;
table[index].begin = nullptr;
return;
}
if (e->next == nullptr) return;
node* tmp = e;
e->next = e->next->next;
delete tmp;
}
V& operator[](const K& key) {
V* v = find(key);
if (v == nullptr) {
push(key, V());
return *find(key);
}
return *v;
}
~HashTable() {
for (uint i = 0;i < size;++i) {
node* e = table[i].begin;
while (e != nullptr) {
node* tmp = e;
e = e->next;
delete tmp;
}
}
delete[] table;
}
private:
static constexpr uint size = 100000;
struct node {
K key;
V value;
node* next;
};
struct list {
node* begin = nullptr, * end = nullptr;
};
list* table;
};
int main()
{
HashTable<int, bool> h;
int t;
fin >> t;
while (t--) {
int v, x;
fin >> v >> x;
if (v == 1) {
h.push(x, true);
}
else if (v == 2) {
h.pop(x);
}
else {
fout << h.exists(x) << '\n';
}
}
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file