Cod sursa(job #2749951)

Utilizator ELEMENTAROSandu Adrian ELEMENTARO Data 9 mai 2021 09:23:52
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.79 kb
#include <iostream>
#include <fstream>
 
using namespace std;
 
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
 
#define cout fout
#define cin fin
 
struct node {
    int key;
    node *next;
};
 
struct list {
    node *head;
};
 
bool get_list(const list &list, int value) {
    node *aux = list.head;
 
    while(aux != NULL) {
        if(aux->key == value) {
            return true;
        }
        aux = aux->next;
    }
 
    return false;
}
 
void init_list(list &list) {
    list.head = NULL;
}
 
void clear_list(list &list) {
    node *aux = list.head;
 
    while(list.head != NULL){
        list.head = list.head->next;
        delete aux;
        aux = list.head;
    }
}
 
void insert_list(list &list, int value) {
    if (get_list(list, value)) {
        return;
    }
 
    node *aux = new node;
    aux->key = value;
 
    aux->next = list.head;
    list.head = aux;
}
 
void delete_list(list &list, int value) {
    if (!get_list(list, value)) {
        return;
    }
 
    node *aux = list.head;
    if (aux->key == value) {
	    list.head = aux->next;
	    delete aux;
	    return;
    }
 
    while(aux->next->key != value) {
        aux = aux->next;
    }
 
    node *aux2 = aux->next;
    aux->next = aux->next->next;
    delete aux2;
}
 
 
struct hash_map {
    const size_t SIZE_HASH = 6291469;
    list *buckets;
};
 
size_t hash_index(hash_map &hash, int key) {
    // niste prelucrari (functii de hash)
    return key % hash.SIZE_HASH;
}
 
bool get_hash(hash_map &hash, int value) {
    if(get_list(hash.buckets[hash_index(hash, value)], value)) {
        return true;
    } else {
        return false;
    }
}
 
void init_hash(hash_map &hash) {
    hash.buckets = new list[hash.SIZE_HASH];
 
    for(int i = 0; i < hash.SIZE_HASH; ++i) {
        init_list(hash.buckets[i]);
    }
}
 
void clear_hash(hash_map &hash) {
    for(int i = 0; i < hash.SIZE_HASH; ++i) {
        clear_list(hash.buckets[i]);
    }

    delete[] hash.buckets;
    hash.buckets = NULL;
}
 
void insert_hash(hash_map &hash, int value) {
    insert_list(hash.buckets[hash_index(hash, value)], value);
}
 
void delete_hash(hash_map &hash, int value) {
    delete_list(hash.buckets[hash_index(hash, value)], value);
}
 
 
void citire_rezolvare(hash_map &hash) {
    int numar_operatii, operatie, value;
 
    cin >> numar_operatii;
    for(int i = 0; i < numar_operatii; ++i) {
        cin >> operatie >> value;
        //cout << '<' << hash_index(hash, value) << '>' << '\n';
 
        if(operatie == 1){
            insert_hash(hash, value);
        } else if(operatie == 2) {
            delete_hash(hash, value);
        } else if(operatie == 3) {
            cout << get_hash(hash, value) << '\n';
        }
    }
}
 
 
int main() {
    hash_map hash;
    init_hash(hash);
 
    citire_rezolvare(hash);
 
    clear_hash(hash);
    return 0;
}