Pagini recente » Cod sursa (job #934066) | Cod sursa (job #2351627) | Cod sursa (job #2549786) | Cod sursa (job #1302278) | Cod sursa (job #1415867)
#include <stdio.h>
#define MAX_N 1000000
#define MOD 666013
#define NIL -1
typedef struct {
int v, next;
} hash;
hash l[MAX_N]; // liste simplu inlantuite alocate static
int lsize;
int h[MOD]; // inceputul listelor alocate static
int st[MAX_N], s; // stiva de pozitii refolosibile
inline int hashFind(int x) {
int i = h[x - (x / MOD) * MOD];
while ((i != NIL) && (l[i].v != x)) {
i = l[i].next;
}
return i;
}
inline void hashInsert(int x) {
int p = s ? st[--s] : lsize++;
int hash = x - (x / MOD) * MOD;
l[p].v = x;
l[p].next = h[hash];
h[hash] = p;
}
inline void hashExtract(int x) {
int hash = x - (x / MOD) * MOD;
int i = h[hash];
if (l[i].v == x) {
h[hash] = l[i].next;
} else {
while ((l[i].next != NIL) && (l[l[i].next].v != x)) {
i = l[i].next;
}
if (l[i].next != NIL) {
st[s++] = l[i].next;
l[i].next = l[l[i].next].next;
}
}
}
int main(void) {
FILE *f, *g;
int testNum;
int x;
for (int i = 0; i < MOD; i++) {
h[i] = NIL;
}
f = fopen("hashuri.in", "r");
fscanf(f, "%d ", &testNum);
g = fopen("hashuri.out", "w");
for (; testNum; testNum--) {
char c = fgetc(f);
fscanf(f, "%d ", &x);
switch (c) {
case '1':
hashInsert(x);
break;
case '2':
hashExtract(x);
break;
case '3':
fputc((hashFind(x) != NIL) + '0', g);
fputc('\n', g);
break;
}
}
fclose(f);
fclose(g);
return 0;
}