Cod sursa(job #481738)

Utilizator edward_alexStanciu Alexandru Marian edward_alex Data 1 septembrie 2010 16:08:44
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include<stdio.h>
#include<stdlib.h>

#define r 666013

typedef struct nod{
	int data;
	struct nod *next;
};

nod *v[r];

void adauga(int x, nod **l){
	nod *t;
	t = (nod*)malloc(sizeof(nod));
	t->data = x;
	t->next = *l;
	*l = t;
}

int cauta(int x,nod *l){
	while(l != NULL){
		if(x == l->data)
			return 1;
		l = l->next;
	}
	return 0;
}

void sterge(int x,nod **l){
	nod *p = *l,*q;
	if(p==NULL)
		return;
	if(p->data == x)
	{
		*l = p->next;
		free(p);
		return;
	}
	while(p->next != NULL){
		if(x == p->next->data){
			q = p->next;
			p->next = q->next;
			free(q);
			return;
		}
		p = p->next;
	}
}

int main(){
	FILE *f,*g;
	int n,i,k,x,t;
	f = fopen("hashuri.in","r");
	g = fopen("hashuri.out","w");
	fscanf(f,"%d",&n);
	for(i = 0;i < n;i++){
		fscanf(f,"%d%d",&k,&x);
		if(k == 1)
			adauga(x,&v[x % r]);
		else if(k == 2)
				sterge(x,&v[x % r]);
		else{
			t = cauta(x,v[x % r]);
			fprintf(g,"%d\n",t);
		}
	}
	fclose(f);
	fclose(g);
	return 0;
}