Cod sursa(job #749419)

Utilizator brainwashed20Alexandru Gherghe brainwashed20 Data 16 mai 2012 22:32:15
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include<fstream.h>

#define H 666013

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

struct Nod {
	int e;
	Nod *urm;
};

Nod *list[H];

void insert(int x) {
	int h;
	h=x%H;
	Nod *q;
	q=new Nod;
	q->e=x;
	q->urm=list[h];
	list[h]=q;
}

int find(int x) {
	int h=x%H;
	Nod *q;
	for(q=list[h]; q; q=q->urm)
	if(x==q->e) 
		return 1;
	return 0;
}

void del(int x) {
	Nod *q;
	int h=x%H;
	if(list[h]->e == x) {
		Nod *q;
		q=list[h];
		list[h]=list[h]->urm;
		delete(q);
		return;
	}
 
	for(q=list[h];q->urm;q=q->urm) {
		if(q->urm->e == x) {
			Nod *t=q->urm;
			q->urm = q->urm->urm;
			delete(t);
			break;
		}
	}
}

int main()	{
	
	int op, x, t;
	f>>t;
	
	while(t--) {
		f>>op>>x;
		if(op==1) {
		   if(!find(x)) 
			   insert(x);
	    }
		else 
			if(op==2) {
				if(find(x)) 
					del(x);
		    }
		    else 
				g<<find(x);
	}
	
	f.close();
	g.close();
	
	return 0;
}