Cod sursa(job #2849872)

Utilizator DMR6476Erdic Dragos DMR6476 Data 15 februarie 2022 21:28:45
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.41 kb
#include <iostream>
#include<fstream>
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
const int MODULO = 500007;
struct node
{
    int value;
    node* theNeighbour;
    node()
    {
        theNeighbour = 0;
    }
};
node* theArray[MODULO + 5];

//numarul ori ultima cifra, tot MODULO
int getHashedValue(int x)
{
    return ((x * (x % 10 + 1)) % (MODULO));
}
void addAtIndex(int index,int theValue)
{

    bool wasFound = false;
    while(theArray[index] != NULL && wasFound == false)
    {
        if(theArray[index] ->value == theValue)
        {
            wasFound = true;
        }
        theArray[index] = theArray[index] ->theNeighbour;

    }

    if(wasFound == false)
    {
        node* newNode = new node();
        newNode ->value = theValue;
        theArray[index] = newNode;
    }

}
void deleteAtIndex(int index,int theVal)
{
    node* lastNode = theArray[index];
    bool wasFound = false;
    while(theArray[index] != NULL && wasFound == false)
    {
        if(theArray[index] ->value == theVal)
        {

            lastNode ->theNeighbour = theArray[index] ->theNeighbour;
            delete theArray[index];
            wasFound = true;
        }
        lastNode = theArray[index];
        theArray[index] = theArray[index] ->theNeighbour;
    }

}
bool findAtIndex(int index,int theVal)
{
    node* currNode = theArray[index];
    bool wasFound = false;

    while(currNode != NULL && wasFound == false)
    {


        if(currNode ->value == theVal)
        {
            wasFound = true;
        }
        currNode = currNode ->theNeighbour;
    }
    return wasFound;
}
int main()
{
    int cases;

    fin>>cases;
    int cMax = cases;
    while(cases --)
    {

        int operation,nr;
        fin>>operation>>nr;
        int index = getHashedValue(nr);

        if(operation == 1)
        {
            //add at 'index'  if, while going through the linked list, you don't find that value
            addAtIndex(index,nr);


        }
        if(operation == 2)
        {
            //delete if you find the nr at position index in the array
            deleteAtIndex(index,nr);
        }
        if(operation == 3)
        {

            //type 1 or 0 if you find the nr at position index in the array
            fout<<findAtIndex(index,nr)<<"\n";
        }
    }
    return 0;
}