Pagini recente » Cod sursa (job #1059174) | Cod sursa (job #1042464) | Cod sursa (job #1317971) | Cod sursa (job #877211) | Cod sursa (job #2634338)
#include <fstream>
class HashTable
{
private:
typedef struct Node
{
long long int value;
Node* next;
}Node;
long long unsigned m, size;
Node** elems;
public:
HashTable()
{
this->m = 666013;
this->size = 0;
this->elems = new Node*[this->m]();
}
~HashTable()
{
for (int i = 0; i < this->m; i++)
{
Node* current = this->elems[i];
while (current != nullptr)
{
Node* next = current->next;
delete current;
current = next;
}
}
delete this->elems;
}
void add(int elem)
{
int bucket = this->hash(elem);
Node* node = new Node;
node->value = elem;
node->next = nullptr;
if (this->elems[bucket] == nullptr)
{
this->elems[bucket] = node;
return;
}
Node* current = this->elems[bucket];
while (current != nullptr)
if (current->value == elem)
return;
node->next = this->elems[bucket];
this->elems[bucket] = node;
}
void remove(int elem)
{
int bucket = this->hash(elem);
Node* current = this->elems[bucket];
Node* prev = nullptr;
while (current != nullptr && current->value != elem)
current = current->next;
if (current == nullptr)
return;
if (prev == nullptr)
{
this->elems[bucket] = current->next;
delete current;
return;
}
prev->next = current->next;
delete current;
}
int search(int elem)
{
int bucket = this->hash(elem);
Node* current = this->elems[bucket];
while (current != nullptr && current->value != elem)
current = current->next;
if (current == nullptr)
return 0;
return 1;
}
private:
int hash(int elem)
{
return elem % this->m;
}
/*
void resize()
{
if (this->m != this->size)
return;
this->m *= 2;
Node** newElems = new Node*[this->m];
for (int i = 0; i < this->size; i += 1)
{
Node* current = this->elems[i];
while (current != nullptr)
{
int bucket = this->hash.
}
}
delete this->elems;
this->elems = newElems;
}
*/
};
int main()
{
HashTable hashTable = HashTable{};
std::ifstream fin{ "hashuri.in" };
std::ofstream fout{ "hashuri.out" };
int commands;
fin >> commands;
for (int i = 0; i < commands; i++)
{
int command, number;
fin >> command >> number;
switch (command)
{
case 1:
{
hashTable.add(number);
break;
}
case 2:
{
hashTable.remove(number);
break;
}
case 3:
{
fout << hashTable.search(number)<<'\n';
break;
}
default:
break;
}
}
}