Pagini recente » Cod sursa (job #1822483) | Cod sursa (job #2057842) | Cod sursa (job #1448007) | Cod sursa (job #2352327) | Cod sursa (job #3128913)
#include <iostream>
#include <fstream>
std::ifstream f("hashuri.in");
std::ofstream g("hashuri.out");
struct Node {
int value;
Node *next;
Node() {
this->value = 0;
this->next = NULL;
}
Node(int value) {
this->value = value;
this->next = NULL;
}
};
class Hash {
private:
Node* v;
const int mod = 666013;
public:
Hash();
void print(int index);
void insert(int);
void remove(int);
bool search(int);
};
Hash::Hash() {
v = new Node[666013];
for(int i=0;i<666013;i++) v[i].value = 0, v[i].next = NULL;
}
void Hash::insert(int value) {
int hash_value = value % mod;
if(v[hash_value].value == 0) v[hash_value].value = value;
else {
bool found = false;
Node *temp = new Node(value);
Node *it = &v[hash_value];
while (it->next != NULL) {
if (it->value == value) {found = true; break;}
it = it->next;
}
if(found == false && it->value != value) it->next = temp;
}
}
void Hash::remove(int value) {
int hash_value = value % mod;
if(v[hash_value].value == 0) return;
if(v[hash_value].value == value) {
v[hash_value].value = 0;
}
else {
Node *it = &v[hash_value];
while(it->next != NULL && it->next->value != value) it = it->next;
if(it->next->value == value) {
Node *temp = it->next;
it->next = it->next->next;
delete temp;
}
}
}
bool Hash::search(int value) {
int hash_value = value % mod;
if(v[hash_value].value == 0)return 0;
if(v[hash_value].value == value) return 1;
Node *it = &v[hash_value];
while(it->next != NULL && it->next->value != value) it = it->next;
if(it->next->value == value) return 1;
return 0;
}
void Hash::print(int index) {
for(int i=1;i<=index;i++)
std::cout<<v[i].value<<" ";
}
int main() {
Hash h;
int n,option,number;
f>>n;
for(int i=0;i<n;i++) {
f>>option>>number;
switch (option)
{
case 1:
h.insert(number);
break;
case 2:
h.remove(number);
break;
case 3:
g<<h.search(number)<<"\n";
break;
default:
break;
}
}
return 0;
}