Cod sursa(job #2746446)

Utilizator ELEMENTAROSandu Adrian ELEMENTARO Data 27 aprilie 2021 21:27:24
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.06 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;

    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 = 10000;
    list *buckets;
};

size_t hash_index(hash_map &hash, int value) {
    // niste prelucrari (functii de hash)
    return value % 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]);
    }
}

void insert_hash(hash_map &hash, int value) {

    if(get_hash(hash, value)) {
        insert_list(hash.buckets[hash_index(hash, value)], value);
    }
}

void delete_hash(hash_map &hash, int value) {

    if(get_hash(hash, 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){
            if(!get_hash(hash, value)){
                insert_hash(hash, value);
            }
        } else if(operatie == 2) {
            if(get_hash(hash, value)){
                delete_hash(hash, value);
            }
        } else if(operatie == 3) {
            if(get_hash(hash, value)){
                cout << 1 << '\n';
            } else {
                cout << 0 << '\n';
            }
        }
    }
}


int main() {
    hash_map hash;
    init_hash(hash);

    citire_rezolvare(hash);

    clear_hash(hash);
    return 0;
}