Pagini recente » Cod sursa (job #416873) | Cod sursa (job #2316254) | Cod sursa (job #1155544) | Cod sursa (job #1727396) | Cod sursa (job #245693)
Cod sursa(job #245693)
#include <iostream>
#include <vector>
#include <list>
#define NMAX 666013
using namespace std;
typedef struct lista{
int valoare;
lista *urm;
}*pLista;
pLista a[NMAX];
int N, x, val;
FILE *f = fopen("hashuri.in", "r"), *g = fopen("hashuri.out", "w");
int find(int x)
{
int list = x % NMAX;
pLista temp;
temp = a[list];
while (temp)
{
if (temp->valoare == x)
return 1;
temp = temp->urm;
}
return 0;
}
int add(int x)
{
int list = x % NMAX;
if (!find(x))
{
lista *nod = new lista;
nod->valoare = list;
nod->urm = a[list];
a[list] = nod;
}
return 0;
}
int del(int x)
{
int list = x % NMAX;
pLista temp = a[list];
if (!find(x))
return 0;
if (temp->valoare == x)
{
a[list] = temp->urm;
delete temp;
//return 0;
}
while (temp->urm)
{
if (temp->urm->valoare == x)
{
pLista p = temp->urm;
temp->urm = temp->urm->urm;
delete p;
//return 0;
}
temp = temp->urm;
}
return 0;
}
int main()
{
fscanf(f, "%d\n", &N);
for (int i = 0; i < N; ++i)
{
fscanf(f, "%d %d", &x, &val);
if (x == 1)
{
add(val);
continue;
}
if (x == 2)
{
del(val);
continue;
}
fprintf(g, "%d\n", find(val));
}
fclose(f);
fclose(g);
return 0;
}