Pagini recente » Cod sursa (job #2810483) | Cod sursa (job #1913746) | Cod sursa (job #2655200) | Cod sursa (job #2152779) | Cod sursa (job #561291)
Cod sursa(job #561291)
#include <stdio.h>
#include <stdlib.h>
#define H 11
typedef struct nod {
int val;
nod * leg;
}nod;
typedef nod* set[H];
int hash_id (int x)
{
return x%H;
}
void init (set h)
{
for(int i = 0; i< H;h[i++]=NULL);
}
int contain (set h, int val){
nod *p;
p = h[hash_id(val)];
while(p != NULL && p->val != val)
p = p->leg;
return p!=NULL;
}
void add (set h, int val)
{
nod *p;
int k = hash_id(val);
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;
p = h[ hash_id(val) ];
if(p != NULL &&p->val == val){
q=p;
p = p->leg;
free(q);
return;
}
while(p != NULL && p->leg->val != val)
p = p->leg;
if(p!=NULL){
q = p->leg;
p->leg=p->leg->leg;
free(q);
}
}
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;
}