Pagini recente » Cod sursa (job #2425989) | Cod sursa (job #847232) | Cod sursa (job #686476) | Cod sursa (job #1165092) | Cod sursa (job #1433705)
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
using namespace std;
#define DIM 666013
struct nod
{
int info;
nod* nod_urm;
} Nod;
void addNode(int _info, struct nod* &radacina)
{
struct nod* n = radacina;
bool gasit = false;
if(n != NULL)
{
while(n->nod_urm && !gasit)
{
n = n->nod_urm;
if(n->info == _info)
gasit = true;
}
if(!gasit)
{
struct nod* nNou;
nNou = new struct nod;
nNou->info = _info;
n->nod_urm = nNou;
nNou->nod_urm = NULL;
}
} else
{
radacina = new struct nod;
radacina->nod_urm = NULL;
radacina->info = _info;
}
}
void deleteNode(int _info, struct nod* &radacina)
{
struct nod* n = radacina;
struct nod* prev = NULL;
bool bRes = true;
bool gasit;
if(n == NULL)
bRes = false;
if(bRes){
if(n->info == _info)
gasit = true;
else
gasit = false;
}
if(bRes)
{
while(n->info != _info && n!=NULL)
{
prev = n;
n = n->nod_urm;
}
}
if(bRes)
{
if(n!=NULL)
gasit = true;
}
if(bRes)
{
if(gasit)
{
if(prev != NULL)
{
prev->nod_urm = n->nod_urm;
delete n;
} else
{
radacina = radacina->nod_urm;
delete n;
}
}
}
}
void list(struct nod* radacina)
{
struct nod* n = radacina;
while(n)
{
cout<<n->info<<" ";
n = n->nod_urm;
}
cout<<endl;
}
bool find( int _info, struct nod* radacina)
{
bool gasit = false;
while(radacina!=NULL && !gasit)
{
if(radacina->info == _info)
gasit = true;
else
radacina = radacina->nod_urm;
}
return gasit;
}
int main()
{
int N;
fstream fin("hashuri.in");
ofstream fout;
fout.open("hashuri.out");
struct nod* radacina[DIM];
struct nod* head[DIM];
memset(radacina, 0, DIM * sizeof(struct nod*));
memset(head, 0, DIM * sizeof(struct nod*));
fin>>N;
for(int i=0;i<N;i++)
{
int a,b;
fin>>a>>b;
if(a == 1)
{
addNode(b, radacina[b%DIM]);
}
else if(a == 2)
{
deleteNode(b, radacina[b%DIM]);
} else if (a == 3)
{
if(find(b, radacina[b%DIM]))
{
#ifdef DEBUG
cout<<b<<" gasit"<<endl;
#endif
fout<<"1";
}
else
{
#ifdef DEBUG
cout<<b<<" negasit"<<endl;
#endif
fout<<"0";
}
if(i<N-1)
fout<<endl;
}
}
fin.close();
fout.close();
return 0;
}