Cod sursa(job #1473721)

Utilizator aimrdlAndrei mrdl aimrdl Data 20 august 2015 00:27:05
Problema Hashuri Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 1.17 kb
#include <stdio.h>
#include <stdlib.h>

#define MAX 1000003

typedef struct list {
	unsigned int n;
	struct list *next;
} Cell, *List;
	
typedef struct {
	List *lists;
	int n;
} Hash;


List * find (List *aL, unsigned int x) {
	while (*aL && (x > (*aL)->n)) {
		aL = &(*aL)->next;
	}
	
	return aL;
}

void add (List *aL, unsigned int x) {
	aL = find(aL, x);
	
	if (*aL && (*aL)->n == x) return;
	
	List temp = malloc(sizeof(Cell));
	temp->next = *aL;
	temp->n = x;
	*aL = temp;
}

void delete (List *aL, unsigned int x) {
	aL = find(aL, x);
	
	if ((*aL) && (*aL)->n == x) {
		List temp = *aL;
		*aL = (*aL)->next;
		free(temp);
	}
}

int hashF (int x) {
	return x % MAX;
}

int main(void) {
	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);
	
	Hash h;
	h.lists = calloc(MAX, sizeof(List));
	h.n = MAX;
	
	int n;
	scanf("%d", &n);
	
	for (int i = 0; i < n; ++i) {
		unsigned int op, x;
		scanf("%u %u", &op, &x);
		
		int p = hashF(x);
		if (op == 1) {
			add(&h.lists[p], x);
		} else if (op == 2) {
			delete(&h.lists[p], x);
		} else {
			List L = *find(&h.lists[p], x);
			if (L && L->n == x) {
				printf("1\n");
			} else {
				printf("0\n");
			}
		}
	}
	
	return 0;
}