Cod sursa(job #1833828)

Utilizator crazylamaRiclea Andrei crazylama Data 23 decembrie 2016 12:27:05
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.73 kb
#include <fstream>
#include <vector>
#define HASHMAX 100009
using namespace std;

ifstream f("hashuri.in");
ofstream g("hashuri.out");

struct Nod
{
    int inf;
    Nod* next;
};

typedef struct Nod* Lista;

int func_dispers(int k)
{
    return k % HASHMAX;
}

void insert_hash_element(int k, vector<Lista> &Hash)
{
    int t = func_dispers(k);
    Lista p = new Nod;
    p -> inf = k;
    p -> next = Hash[t];
    Hash[t] = p;
}

bool cautare_hash_element(int k, vector<Lista> Hash)
{
    int t = func_dispers(k);
    Lista p = Hash[t];
    while (p != NULL && p -> inf != k)
        p = p -> next;
    if (p != NULL)
        return 1;
    return 0;
}

void sterge_hash_element(int k, vector<Lista> &Hash)
{
    int t = func_dispers(k);
    Lista p = Hash[t];
    if (p == NULL)
        return;
    if (p -> inf == k)
    {
        Hash[t] = p -> next;
        delete p;
    }
    else
    {
        Lista q = p -> next;
        while (q != NULL && q -> inf != k)
        {
            q = q -> next;
            p = p -> next;
        }
        if (q != NULL)
        {
            p -> next = q -> next;
            delete q;
        }
    }
}

int main()
{
    int n;
    f >> n;
    vector<Lista> Hash;
    Hash.resize(HASHMAX);
    for (vector<Lista>::iterator i = Hash.begin(); i <= Hash.end(); ++i)
        *i = NULL;
    for (int i = 0; i < n; ++i)
    {
        int cod, x;
        f >> cod >> x;
        if (cod == 1)
            insert_hash_element(x, Hash);
        else if (cod == 2)
            sterge_hash_element(x, Hash);
        else if (cod == 3)
            g << cautare_hash_element(x, Hash) << "\n";
    }
    f.close();
    g.close();
    return 0;
}