Cod sursa(job #329293)

Utilizator xaphariusMihai Suteu xapharius Data 5 iulie 2009 18:38:46
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#define HASH_TABLE_SIZE 1000000
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>

struct nod{
	int val;
	nod* next;
};

nod* vec[HASH_TABLE_SIZE];

int H(int key){
	int index; 
	index = (index >> 28) ^ index;
	index = ((index >> 16) & 0xf00)^ index; 
	index = ((index >> 8) & 0xf0000)^ index; 
	index = index % 1000000;
	return index;
}

bool find(int key){
	int index = H(key);
	if (!vec[index]) return 0;
	
	nod *q = vec[index];
	for (; q && q->val != key; q = q->next);
	
	return q;
}

void ins(int key){
	if (find(key)) return;
	nod *p = new nod;
	p->val = key;
	p->next = NULL;

	int index = H(key);
	if ( vec[index] ){
		nod *q = vec[index];
		for (; q->next; q = q->next);
		q->next = p;
	}
	else 
		vec[index] = p;
}

void del(int key){
	int index = H(key);
	if (!find(key)) return;
	if (vec[index]->val == key) {
		nod *p = vec[index];
		vec[index] = vec[index]->next;
		delete(p);
	}
	else {
		nod *q = vec[index];
		for (; q->next->val != key; q = q->next);
		
		nod *p = q->next;
		q->next = p->next;
		delete(p);
	}
}

int main(){
	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);
	int n, op, key;
	for (scanf("%d", &n); n; --n){
		scanf("%d %d", &op, &key);
		switch (op){
			case 1: ins(key);
				break;
			case 2: del(key);
				break;
			case 3: printf("%d\n", find(key));
				break;
		}
	}

	return 0;
}