Pagini recente » Cod sursa (job #2890227) | Cod sursa (job #2232105) | Cod sursa (job #847955) | Cod sursa (job #1112382) | Cod sursa (job #472426)
Cod sursa(job #472426)
// Hashuri.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
FILE *f=fopen("hashuri.in", "r");
FILE *g=fopen("hashuri.out", "w");
int n;
typedef struct nod
{
int x;
struct nod *adr;
};
nod *l[65538];
void inserare(int x)
{
int cv=x%65537;
if (l[cv]==NULL)
{
l[cv]=new nod;
l[cv]->x=x;
l[cv]->adr=NULL;
}
else
{
nod *p=new nod;
p->x=x;
p->adr=l[cv];
nod *q=new nod;
q=l[cv];
while (q->x!=x && q!=NULL)
{
q=q->adr;
}
if (q==NULL)
l[cv]=p;
}
}
void stergere(int x)
{
int cv=x%65537;
if (l[cv]==NULL) return;
else
{
nod *p=new nod;
nod *q=new nod;
p=l[cv];
if (p->x==x)
{
l[cv]=p->adr;
delete p;
}
else
{
while (p->x!=x && p!=NULL)
{
q=p;
p=p->adr;
}
if (p->x==x)
{
q->adr=p->adr;
delete p;
}
}
}
}
int cauta(int x)
{
int cv=x%65537;
if (l[cv]==NULL)
{
return 0;
}
else
{
nod *q=new nod;
q=l[cv];
while (q->x!=x && q!=NULL)
{
q=q->adr;
}
if (q==NULL)
return 0;
return 1;
}
}
void program()
{
fscanf(f, "%d", &n);
for (int i=1; i<=n; i++)
{
int x=0, y=0;
fscanf(f, "%d%d", &x, &y);
if (x==1)
inserare(y);
else if (x==2)
stergere(y);
else
fprintf(g, "%d\n", cauta(y));
}
}
int main()
{
program();
return 0;
}