Cod sursa(job #1181544)

Utilizator tudi98Cozma Tudor tudi98 Data 3 mai 2014 01:44:53
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include<fstream>
#include<stdlib.h>
#include<stdio.h> 
#define dim 666015
#define P 666013
using namespace std;

typedef struct list{
	int K;
	struct list* next;
} List;
typedef List* Hash[dim];

Hash H;

inline void inithash(){
	for(int i=0;i<dim;H[i++]=NULL);
}


inline int h(int val){
	return val % P;
}

void Add(int val){
	List* L=(List*)malloc(sizeof(List));
	L->K=val;
	L->next=H[h(val)];
	H[h(val)]=L;
}

bool search(int val){
	List* L;
	for(L=H[h(val)];L && L->K!=val;L=L->next);
	return L != NULL;
}

void del(int val){
	List* L;
	for(L=H[h(val)];L && L->K!=val;L=L->next);
	if(L != NULL){
		if(L->next==NULL) free(L);
		else{
			L->K=L->next->K;
			L->next=L->next->next;
		}
	}
}


int main(){

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

	int x,tip,i,n;
	f>>n;

	inithash();
	for(i=1;i<=n;i++){
		f >> tip >> x;
		if(tip==1){
			Add(x);
		}
		if(tip==2){
			del(x);
		}
		if(tip==3){
			g << search(x) << "\n";
		}
	}
}