Pagini recente » Cod sursa (job #2561112) | Cod sursa (job #434660) | Cod sursa (job #2298716) | Monitorul de evaluare | Cod sursa (job #869850)
Cod sursa(job #869850)
#include <stdio.h>
#include <stdlib.h>
typedef struct nodes
{
int info;
struct nodes *next;
} node;
#define MAX 300000
node *lst[MAX];
int n;
int add(int nr)
{
int ind = nr % MAX;
node *elm, *crt;
crt = lst[ind];
while (crt != NULL && crt->info != nr)
crt = crt->next;
if (crt == NULL)
{
elm = (node *) malloc(sizeof(node));
if (!elm)
{
return -1;
}
elm->info = nr;
elm->next = lst[ind];
lst[ind] = elm;
return 1;
}
return 0;
}
int del(int nr)
{
int ind = nr % MAX;
node *crt, *elm;
crt = lst[ind];
if (crt != NULL)
{
if (crt->info == nr)
{
lst[ind] = crt->next;
free(crt);
}
else
{
while (crt->next != NULL && crt->next->info != nr)
crt = crt->next;
if (crt->next != NULL && crt->next->info == nr)
{
elm = crt->next;
crt->next = elm->next;
free(elm);
return 1;
}
}
}
return 0;
}
int sch(int nr)
{
int ind = nr % MAX;
node *crt = lst[ind];
while (crt != NULL && crt->info != nr)
crt = crt->next;
if (crt == NULL)
return 0;
return 1;
}
int main()
{
FILE *fin, *fout;
int op, i, nr;
fin = fopen("hashuri.in", "r");
fout = fopen("hashuri.out", "w");
if (!fin || !fout)
{
return 1;
}
fscanf(fin, "%d", &n);
for (i=0; i<n; i++)
{
fscanf(fin, "%d %d", &op, &nr);
switch (op)
{
case 1:
add(nr);
break;
case 2:
del(nr);
break;
case 3:
fprintf(fout, "%d\n", sch(nr));
break;
}
}
fclose(fin);
fclose(fout);
return 0;
}