Pagini recente » Cod sursa (job #2663169) | Cod sursa (job #655421) | Cod sursa (job #2954369) | Cod sursa (job #1042560) | Cod sursa (job #2637926)
// HashTable.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
template<class T>
class Hash {
public:
static constexpr int SIZE = 1000000;
struct node {
T x;
node* next = nullptr, * prev = nullptr;
};
struct list {
node* begin = nullptr, * end = nullptr;
}hash[SIZE];
bool exists(T x) {
int index = x % SIZE;
node* e = hash[index].begin;
while (e != nullptr) {
if (e->x == x)
return true;
e = e->next;
}
return false;
}
void add(T x) {
if (exists(x)) return;
int index = x % SIZE;
node* e = new node;
e->x = x;
if (hash[index].begin == nullptr) {
hash[index].begin = e;
}
else {
hash[index].end->next = e;
e->prev = hash[index].end;
}
hash[index].end = e;
}
void erase(T x) {
if (!exists(x)) return;
int index = x % SIZE;
node* e = hash[index].begin;
while (e->x != x) {
e = e->next;
}
if (e->prev != nullptr)
e->prev->next = e->next;
if (e->next != nullptr)
e->next->prev = e->prev;
if (e == hash[index].begin)
hash[index].begin = e->next;
else if (e == hash[index].end)
hash[index].end = e->prev;
delete e;
}
};
int main()
{
Hash<int> h;
int t;
float max = 0;
fin >> t;
while (t--) {
clock_t T = clock();
int v, x;
fin >> v >> x;
if (v == 1) {
h.add(x);
}
else if (v == 2) {
h.erase(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