Cod sursa(job #1979455)

Utilizator StefanMoisanuMoisanu Stefan StefanMoisanu Data 10 mai 2017 17:22:27
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <iostream>
#include <fstream>
#define p 666019
using namespace std;
ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");
struct nod;
bool caut (int x, nod* last);
struct nod{
    int inf;
    nod* urm;
} *(L[p]);

void sterge (int x){
    nod *previous, *curent;
    curent = L[x % p];
    if (curent == NULL) return;
    if (curent -> urm == NULL && curent -> inf == x) {
            L[x%p]=NULL;
            delete curent;
            return;
    }
    while (curent != NULL){
        previous = curent;
        curent = curent -> urm;
        if (curent ->inf == x){
            previous -> urm = curent -> urm;
            delete curent;
            return;
        }
    }
}
void adauga (int x){
    nod *last = NULL;
    if (caut(x, last)==false){
       if (last == NULL){
          L[x % p] = new nod;
          L[x % p] -> inf = x;
          L[x % p] ->urm =NULL;
       }
       else{
        last -> urm = new nod;
        last -> inf = x;
        last ->urm ->urm = NULL;
       }
    }
}
bool caut (int x, nod *last = NULL){
    int hashedX = x% p;
    nod* curent = L[hashedX];
    while(curent != NULL){
        if (curent -> inf == x)
           return true;
        last = curent;
        curent = curent -> urm;
    }
    return false;
}

int main()
{
    int N,op,x,i;
    for (i=0; i<p; i++)
        L[i]= NULL;
    fin>>N;
    for (i=1;i<=N;i++){
        fin>>op>>x;
        if (op==2)
            sterge (x);
        if (op==1)
            adauga(x);
        if (op==3)
            fout<<caut(x)<<'\n';
    }
    return 0;
}