Cod sursa(job #3136435)

Utilizator AlexTrandafir09Trandafir Alexandru Ionut AlexTrandafir09 Data 6 iunie 2023 12:22:28
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.06 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
struct Nod {
    int valoare;
    Nod *urm_nod;

    Nod() {
        valoare = 0;
        urm_nod = nullptr;
    }

    Nod(int valoare) {
        this->valoare = valoare;
        urm_nod = nullptr;
    }

    Nod& operator=(const Nod& alt_nod){
        if(this != &alt_nod) {
            this->valoare = alt_nod.valoare;
            this->urm_nod = alt_nod.urm_nod;
        }
        return *this;
    }
};

class Hash {
private:
    Nod* v;
    const int mod = 666013;

public:
    Hash() {
        v = new Nod[666013];
        for(int i=0;i<666013;i++) {
            v[i].valoare = 0;
            v[i].urm_nod = nullptr;
        }
    }
    void inserare(int valoare) {
        int cheie_hash = valoare % mod;
        if(v[cheie_hash].valoare == 0) v[cheie_hash].valoare = valoare;
        else {
            bool ok= false;
            Nod *temp = new Nod(valoare);
            Nod *actual = &v[cheie_hash];
            while (actual->urm_nod != nullptr) {
                if (actual->valoare == valoare){
                    ok = true;
                    break;
                }
                actual = actual->urm_nod;
            }
            if(ok == false && actual->valoare != valoare)
                actual->urm_nod = temp;
        }
    }
    void stergere(int valoare) {
        int cheie_hash = valoare % mod;
        if(v[cheie_hash].valoare == 0) return;
        if(v[cheie_hash].valoare == valoare && v[cheie_hash].urm_nod != nullptr) {
            Nod* temp = v[cheie_hash].urm_nod;
            v[cheie_hash] = *temp;
        }
        else if(v[cheie_hash].valoare == valoare && v[cheie_hash].urm_nod == nullptr)  {
            v[cheie_hash].valoare = 0;
        }
        else {
            Nod *actual = &v[cheie_hash];
            while(actual->urm_nod != nullptr && actual->urm_nod->valoare != valoare)
                actual = actual->urm_nod;
            if(actual->urm_nod != nullptr && actual->urm_nod->valoare == valoare) {
                if(actual->urm_nod->urm_nod == nullptr)
                    actual->urm_nod = nullptr;
                else
                    actual->urm_nod = actual->urm_nod->urm_nod;
            }
        }
    }
    bool cautare(int valoare_cautata) {
        cout << valoare_cautata << "\n";
        int cheie_hash = valoare_cautata % mod;
        if(v[cheie_hash].valoare == 0) return 0;
        if(v[cheie_hash].valoare == valoare_cautata) return 1;
        Nod *actual = &v[cheie_hash];
        while(actual->urm_nod != nullptr && actual->urm_nod->valoare != valoare_cautata)
            actual = actual->urm_nod;
        if(actual->urm_nod != nullptr && actual->urm_nod->valoare == valoare_cautata)
            return 1;
        return 0;
    }
};
int main() {
    Hash h;
    int n,alegere,nr;
    f>>n;
    for(int i=0;i<n;i++) {
        f >> alegere >> nr;
        if(alegere==1)
                h.inserare(nr);
        if(alegere==2)
                h.stergere(nr);
        if(alegere==3)
            g << h.cautare(nr) << "\n";
    }
    return  0;
}