Pagini recente » Cod sursa (job #3274686) | Cod sursa (job #3274207) | Cod sursa (job #3268822) | Cod sursa (job #3268401) | Cod sursa (job #729177)
Cod sursa(job #729177)
#include <stdio.h>
#include <stdlib.h>
#define MOD 666667
int hashFunction (int key)
{
return key % MOD;
}
typedef struct nod
{
int key;
struct nod *next;
} NodeT;
NodeT *BucketTable[MOD] = {NULL};
void add(int x, NodeT *&p)
{
if(p == NULL)
{
p = (NodeT*)malloc(sizeof(NodeT));
p->next = NULL;
p->key = x;
return ;
}
NodeT *t = NULL;
t = (NodeT*)malloc(sizeof(NodeT));
t->key = x;
t->next = p;
p = t;
}
int search(int x, NodeT *p)
{
if (p == NULL)
return 0;
NodeT *t = NULL;
for(t = p; t ; t=t->next)
if(t->key == x)
return 1;
return 0;
}
void remove(int x, NodeT *&p)
{
if (p == NULL)
return;
if (p->key == x)
{
NodeT *t = p;
p = p->next;
free(t);
return;
}
NodeT *t = p;
for(t = p; t->next != NULL; t = t->next)
if(t->next->key == x)
{
NodeT *t2 = t->next;
t->next = t2->next;
free(t2);
}
}
int main()
{
int n, i, op, x;
FILE *f = fopen("hashuri.in", "r");
FILE *fout = fopen("hashuri.out", "w");
fscanf(f, "%d", &n);
for(i = 0; i < n; ++i)
{
fscanf(f, "%d%d", &op, &x);
switch (op)
{
case 1:
{
if (!search(x, BucketTable[ hashFunction(x)] ))
add(x, BucketTable[ hashFunction(x) ]);
break;
}
case 2:
{
remove(x, BucketTable[ hashFunction(x) ]);
break;
}
case 3:
{
fprintf(fout, "%d\n", search(x, BucketTable[ hashFunction(x) ]));
break;
}
}
}
fclose(f);
fclose(fout);
return 0;
}