Cod sursa(job #2293945)

Utilizator IordachescuAncaFMI Iordachescu Anca Mihaela IordachescuAnca Data 1 decembrie 2018 18:49:43
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.67 kb
#include<fstream>
#define M 393241
using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

struct node
{
    int info;
    node *urm;
};
node *hashtable[M];

int n,i,digit,value;

int hfunction(int x)
{
    return x%M;
}

int searchnode(int value)
{
    node *head = hashtable[hfunction(value)];

    while (head != NULL && head->info != value)
    {
        head = head->urm;
    }

    if (head != NULL)
    {
        return 1;
    }
    return 0;
}

void addnode(int value)
{
    node *first = new node;
    first->info = value;
    first->urm = hashtable[hfunction(value)];
    hashtable[hfunction(value)] = first;
}

void deletenode(int value)
{
    if (hashtable[hfunction(value)]->info == value)
    {
        hashtable[hfunction(value)] = hashtable[hfunction(value)]->urm;
    }
    else
    {
        node *first = hashtable[hfunction(value)];
        while (first->urm->info != value)
        {
            first = first->urm;
        }
        node *deleted = first->urm;
        first->urm = deleted->urm;
        delete deleted;
    }
}
int main()
{
    fin >> n;

    for (i = 1; i <= n; i++)
    {
        fin >> digit >> value;

        if (digit == 1)
        {
            if (searchnode(value) == 0)
            {
                addnode(value);
            }
        }

        if (digit == 2)
        {
            if (searchnode(value) == 1)
            {
                deletenode(value);
            }
        }

        if (digit == 3)
        {
            fout << searchnode(value) << "\n";
        }
    }

    fin.close();
    fout.close();

    return 0;
}