Cod sursa(job #2108473)

Utilizator Andreiii500Andrei Puiu Andreiii500 Data 18 ianuarie 2018 13:44:47
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

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

const int MOD = 666013;

struct elem{
    int val;
    elem *next;
};

elem *(v[MOD]);

void push(int val){
    int hashIndex = val%MOD;

    elem *newElem = new elem;
    newElem->val = val;
    newElem->next = v[hashIndex];
    v[hashIndex] = newElem;
}

void pop(int val){
    elem *crtElem = v[val%MOD];

    if(!crtElem)
        return;
    else
    {
        if(!crtElem->next)
        {
            if(crtElem->val == val)
                v[val%MOD] = NULL;
        }
        else
        {
            while(crtElem != NULL && crtElem->next != NULL){
                if(crtElem->next->val == val)
                {
                    crtElem->next = crtElem->next->next;
                    return;
                }
                crtElem = crtElem->next;
            }
        }
    }
}

int checkExistence(int val){
    elem *crtElem = v[val%MOD];
    while(crtElem != NULL){
        if(crtElem->val == val)
            return 1;
        crtElem = crtElem->next;
    }
    return 0;
}

int main()
{
    int n;
    in>>n;
    while(n--)
    {
        int op,x;
        in>>op>>x;
        if(op == 1)
        {
            if(!checkExistence(x))
                push(x);
        }
        else if(op == 2)
        {
            ///if(checkExistence(x))
                pop(x);
        }
        else
        {
            out<<checkExistence(x)<<"\n";
        }
    }

    return 0;
}