Pagini recente » Cod sursa (job #3273185) | Cod sursa (job #615336) | Cod sursa (job #882038) | Cod sursa (job #1413885) | Cod sursa (job #561299)
Cod sursa(job #561299)
#include <stdio.h>
#include <stdlib.h>
#define H 999983
typedef struct nod {
int val;
nod * leg;
}nod;
typedef nod* set[H];
void init (set h)
{
for(int i = 0; i< H;h[i++]=NULL);
}
int contain (set h, int val){
nod *p;
p = h[val%H];
while(p != NULL && p->val != val)
p = p->leg;
return p!=NULL;
}
void add (set h, int val)
{
nod *p;
int k = val%H;
if(contain(h,val)) return;
p = new nod;
p ->val = val;
p ->leg = h[k];
h[k] = p;
}
void del(set h, int val)
{
nod *p, *q;
int x;
q = NULL;
p = h[x=val%H];
while(p){
if(p->val == val){
if(q == NULL)
h[x] = p->leg;
else
q->leg = p->leg;
free(p);
return;
}
q=p;
p=p->leg;
}
}
void prelucrare(){
FILE *f, *g;
int op, val, n;
f = fopen("hashuri.in","r");
g = fopen("hashuri.out","w");
fscanf(f,"%d",&n);
set h;
init (h);
while(n--){
fscanf(f,"%d %d",&op,&val);
switch(op){
case 1:
add(h,val);
break;
case 2:
del(h,val);
break;
case(3):
fprintf(g,"%d\n",contain(h,val));
}
}
fclose(f);
fclose(g);
}
int main(){
prelucrare();
return 0;
}