Cod sursa(job #582567)

Utilizator vlad2901Vlad Berindei vlad2901 Data 15 aprilie 2011 16:26:47
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <stdio.h>
#define M 11

int n;

struct node {
	int info;
	node *next;
} **H;

node* search(int x) {
	node *c;
	for(c = H[x%M];c && c->info != x;c=c->next);	
	return c;
}

void add(int x) {
	node *c;
	c = search(x);
	if(c == NULL) {
		c = new node;
		c->info = x;
		c->next = H[x%M];
		H[x%M] = c;
	}		
}

void deleteNode(int x) {
	node *c, *p;
	for(c = H[x%M];c && c->info != x;p=c, c=c->next);
	if(c == H[x%M] && c!= NULL) {
		H[x%M] = c->next;
		delete c;
		return;
	}
	if(c) {
		p->next = c->next;
		delete c;	
	}
}

int main() {
	
	int x,i,op;
	
	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);
	
	scanf("%d", &n);
	H = new node*[M];
    
	for(i=0;i<M;i++) {
		H[i] = NULL;
	}
	
	for(i=0;i<n;i++) {
		scanf("%d %d", &op, &x);
		switch(op) {
			case 1: add(x); break;
			case 2: deleteNode(x); break;
			case 3: printf("%d\n", search(x) == NULL ? 0 : 1); 
		}
	}
    return 0;
    
}