Pagini recente » Cod sursa (job #841327) | Cod sursa (job #841310) | Cod sursa (job #841359) | Cod sursa (job #729320)
Cod sursa(job #729320)
//varianta C pur
#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);
t = NULL;
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);
t2 = NULL;
return;
}
}
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;
}