Cod sursa(job #2850913)

Utilizator DMR6476Erdic Dragos DMR6476 Data 17 februarie 2022 19:20:38
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.43 kb
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
const int MODULO = 500009;
struct node
{
    int value = 0;
    node* neighbour = NULL;
    node()
    {
        value = 0;
        neighbour = NULL;
    }
    node(long long theValue)
    {
        value = theValue;
        neighbour = NULL;
    }
}
;
node* theArray[MODULO + 5];
long long getHashed(long long number)
{
    return ((number % MODULO) *  ((number % 3 ) % MODULO)) % MODULO;
}
void addNumber(long long number,long long index)
{

    bool wasFound = false;
    if(theArray[index] == NULL)
    {
        //add a root to the start
        node* newNode = new node();
        theArray[index] = newNode;
    }
    node* eachOne = theArray[index];

    while(eachOne ->neighbour != NULL)
    {
        eachOne = eachOne ->neighbour;

        if(eachOne ->value == number)
            wasFound = true ;
    }
    if(wasFound == false)
    {


        node* newNode = new node(number);
        newNode ->neighbour = NULL;

        eachOne ->neighbour = newNode;

    }

}
void deleteNumber(long long number, long long index)
{
    if(theArray[index] == NULL) return ;
    node* currOne = theArray[index];
    node* lastOne = NULL;
    bool wasFound = false;
    while(currOne ->neighbour != NULL)
    {
        lastOne = currOne;
        currOne = currOne ->neighbour;

        if(currOne ->value == number)
        {
            wasFound = true;
            break;
        }
    }
    if(wasFound == true)
    {
        lastOne ->neighbour = currOne ->neighbour;
        delete currOne;
    }
}
bool findNumber(long long number, long long index)
{
    node* currOne = theArray[index];
    if(currOne == NULL) return false;
    while(currOne ->neighbour != NULL)
    {
        currOne = currOne ->neighbour;
        if(currOne ->value == number)
        {
            return true;
        }
    }
    return false;
}
int main()
{
    int n;
    fin>>n;
    while(n--)
    {
        int command;
        long long number;
        fin>>command>>number;
        long long index = getHashed(number);
        if(command == 1)
            addNumber(number,index);
        if(command == 2)
            deleteNumber(number, index);
        if(command == 3)
        {
            fout<<findNumber(number, index)<<'\n';
        }
    }
    return 0;
}