Mai intai trebuie sa te autentifici.

Cod sursa(job #714321)

Utilizator bogdanrnRadu Bogdan Nicolae bogdanrn Data 15 martie 2012 17:53:01
Problema Hashuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
struct nod{
int inf;
nod *next;
};
nod *p[666013],*u[666013];
const int CONST=666013;
bool search(int x){
     int poz=x%CONST;
	 nod *par=p[poz];
	while (par!=NULL){

		 if (par->inf==x){
                    return 1;
         }
		 par=par->next;
    }
    return 0;
}
void adauga(int x){
     if (search(x)==0){
	     int poz=x%CONST;
		 nod *aux=new nod;
		 aux->inf=x;
		 aux->next=NULL;
		 if (p[poz]!=NULL){
			u[poz]->next=aux;
			u[poz]=u[poz]->next;
		} else {
			p[poz]=aux;
			u[poz]=p[poz];
		}
    }
}
void sterge(int x){
     int poz=x%CONST;
	 nod *parcurg=p[poz];
	 nod *pa=p[poz];
	 if (parcurg->inf==x){
		p[poz]=parcurg->next;
		delete pa;
		return;
	 }
	 while (parcurg!=NULL){
			if (parcurg->inf==x){
				pa->next=parcurg->next;
				cout<<"sterg "<<x;
				delete parcurg;
				return;
			}
			pa=parcurg;
			parcurg=parcurg->next;
	 }
}

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

    }



    return 0;
}