Cod sursa(job #714765)

Utilizator sternvladStern Vlad sternvlad Data 16 martie 2012 02:31:18
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.56 kb
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
struct nod
{
    int informatie;
    nod *urmator;
}*p[666013],*u[666013];
int NUMAR=666013;
bool cauta(int x)
    {
     int poz=x%NUMAR;
	 nod *par=p[poz];
	while (par!=NULL)
	{
		 if (par->informatie==x){
         return 1;
    }
	 par=par->urmator;
    }
    return 0;
}
void adauga(int x)
{
     if (cauta(x)==0)
     {
	     int poz=x%NUMAR;
		 nod *aux=new nod;
		 aux->informatie=x;
		 aux->urmator=NULL;
		 if (p[poz]==NULL)
		 {
            p[poz]=aux;
			u[poz]=p[poz];
         }
          else
          {
			u[poz]->urmator=aux;
			u[poz]=u[poz]->urmator;
		  }
      }
}
void sterge(int x)
{
     int poz=x%NUMAR;
	 nod *parcurg=p[poz];
	 nod *pa=p[poz];
	 if (parcurg)
	 if (parcurg->informatie==x)
	 {
        if (parcurg->urmator)
		p[poz]=parcurg->urmator;
		else
		p[poz]=NULL;
		delete pa;
		return;
	 }
	 while (parcurg!=NULL)
	 {
			if (parcurg->informatie==x)
			{
				pa->urmator=parcurg->urmator;
				delete parcurg;
				return;
			}
			pa=parcurg;
			parcurg=parcurg->urmator;
	 }
}

int main(int argc, char *argv[])
{
    int i,n,a,operatie;
    in>>n;
    for (i=1;i<=n;i++){
        in>>operatie;
        in>>a;
        if (operatie==1){
                   adauga(a);
        }
        else if (operatie==2)
        {
                sterge(a);
        } else {
                out<<cauta(a)<<"\n";
        }

    }



    return 0;
}