Pagini recente » Cod sursa (job #2704105) | Cod sursa (job #2577996) | Cod sursa (job #2508929) | Cod sursa (job #2838492) | Cod sursa (job #1200456)
#include <iostream>
#include <cstdio>
#include <fstream>
#define HASH_SIZE 666013
using namespace std;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
struct node {
node* next;
int info;
node() {
next = NULL;
info = 0;
}
};
bool lookup(node* hash[], int el) {
node* list = hash[el%HASH_SIZE];
while(list) {
if(list->info == el)
return true;
list = list->next;
}
return false;
}
void add(node* hash[], int el) {
if (lookup(hash, el)) {
return;
}
node* list = hash[el%HASH_SIZE];
node* newnode = new node;
newnode->info = el;
newnode->next = list;
hash[el%HASH_SIZE] = newnode;
}
void remove(node* hash[], int el) {
if (!lookup(hash, el)) {
return;
}
node* list = hash[el%HASH_SIZE];
node* prev = list;
while(list) {
if (list->info == el) {
if(prev == list) {
hash[el%HASH_SIZE] = list->next;
return;
}
prev->next = list->next;
delete list;
return;
}
list = list->next;
}
}
int main (int argc, char const *argv[])
{
int n;
in>>n;
int op, el;
node** hash_table = new node*[HASH_SIZE];
for(int i=0; i<n; ++i) {
in>>op>>el;
if (op == 1)
add(hash_table, el);
if (op == 2)
remove(hash_table, el);
if (op == 3)
out<<lookup(hash_table, el)<<"\n";
}
for(int i=0; i<HASH_SIZE; ++i) {
node* list = hash_table[i];
node* aux;
while(list) {
aux = list;
list = list->next;
delete aux;
}
}
delete hash_table;
return 0;
}