Cod sursa(job #2680095)

Utilizator WilIiamperWilliam Damian Balint WilIiamper Data 2 decembrie 2020 17:22:03
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.32 kb
#include <fstream>

using namespace std;

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

int n;
const int M = 108301;

struct nod {
    int info;
    nod * next;
    nod * prev;
};

nod * HT[M] = {NULL};


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

void operatia1 ( int x ) {

    int index = Hash(x);

    nod * new_node = new nod;
    new_node -> info = x;
    new_node -> next = NULL;
    new_node -> prev = NULL;

    nod * t = HT[index];
    if ( t == NULL )
        HT[index] = new_node;
    else {
        while ( t->next != NULL ) {
            if ( t->info == x ) {
                delete new_node;
                return;
            }
            t = t->next;
        }
        if ( t->info == x ) {
            delete new_node;
            return;
        }

        new_node -> prev = t;
        t->next = new_node;
    }
}

void operatia2 ( int x ) {

    int index = Hash(x);

    nod * t = HT[index];
    if ( t == NULL )
        return;
    else {
        while ( t->next != NULL ) {
            if ( t->info == x ) {
                if ( t->prev != NULL ) {
                    ( t->prev )->next = t->next;
                    ( t->next )->prev = t->prev;
                } else {
                    HT[index] = t->next;
                    (t->next)->prev = NULL;
                }

                delete t;
                return;
            }
            t = t->next;
        }
        if ( t->info == x ) {
            if ( t->prev != NULL )
                ( t->prev )->next = NULL;
            else
                HT[index] = NULL;

            delete t;
            return;
        }
    }
}

bool operatia3 ( int x ) {

    int index = Hash(x);

    nod * t = HT[index];
    if ( t == NULL )
        return false;
    else {
        while ( t->next != NULL ) {
            if ( t->info == x )
                return true;
            t = t->next;
        }
        if ( t->info == x )
            return true;
    }
    return false;
}

int main()
{
    int op, x;
    fin >> n;

    while ( n-- ) {
        fin >> op >> x;
        if ( op == 1 )
            operatia1( x );
        else if ( op == 2 )
            operatia2( x );
        else
            fout << operatia3(x) << "\n";
    }
    return 0;
}