Cod sursa(job #2728490)

Utilizator om6gaLungu Adrian om6ga Data 23 martie 2021 12:36:32
Problema Hashuri Scor 70
Compilator c-32 Status done
Runda Arhiva educationala Marime 1.74 kb
#include <stdio.h>
#include <string.h>

unsigned int lowbias32(unsigned int x) {
    x ^= x >> 16;
    x *= 0x7feb352d;
    x ^= x >> 15;
    x *= 0x846ca68b;
    x ^= x >> 16;
    return x;
}

struct node {
	unsigned int val;
	struct node *next;
};

typedef struct node node;

node mem[1000000];
node *hashtable[512];
node *free_nodes;

void op_add(unsigned int index, unsigned int nr) {
	node *n = free_nodes;
	free_nodes = free_nodes->next;
	n->next = hashtable[index];
	n->val = nr;
	hashtable[index] = n;
}

void op_remove(unsigned int index, unsigned int nr) {
	node *current = hashtable[index];
	node *prev = current;
	node *rem = NULL;

	while (current) {
		if (current->val == nr) {
			break;
		}
		prev = current;
		current = current->next;
	}
	if (current) {
		if (prev == current) { // first node
			hashtable[index] = hashtable[index]->next;
		}
		else {
			prev->next = current->next; // detach node from bucket
		}
		current->next = free_nodes;
		free_nodes = current;
	}
}

void op_question(unsigned int index, unsigned int nr) {
	node *current = hashtable[index];
	while (current) {
		if (current->val == nr) {
			printf("1\n");
			return;
		}
		current = current->next;
	}
	printf("0\n");
}

void main() {
	unsigned int i, o, n;
	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);
	void *op_fct[] = {op_add, op_remove, op_question};
	free_nodes = mem;
    node *fnode = free_nodes;
	for (i = 1; i < 1000000; i++) {
		fnode->next = mem+i;
		fnode = fnode->next;
	}
	
	scanf("%d", &i);
	while (1) {
		i--;
		scanf("%u %u", &o, &n);
		((void (*)(unsigned int, unsigned int))op_fct[o-1])(lowbias32(n) & 0x1ff, n);
		if (i == 0)
	        break;
	}
	return;
}