Pagini recente » Cod sursa (job #1103040) | Cod sursa (job #281325) | Cod sursa (job #1112829) | Cod sursa (job #2240969) | Cod sursa (job #2293945)
#include<fstream>
#define M 393241
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
struct node
{
int info;
node *urm;
};
node *hashtable[M];
int n,i,digit,value;
int hfunction(int x)
{
return x%M;
}
int searchnode(int value)
{
node *head = hashtable[hfunction(value)];
while (head != NULL && head->info != value)
{
head = head->urm;
}
if (head != NULL)
{
return 1;
}
return 0;
}
void addnode(int value)
{
node *first = new node;
first->info = value;
first->urm = hashtable[hfunction(value)];
hashtable[hfunction(value)] = first;
}
void deletenode(int value)
{
if (hashtable[hfunction(value)]->info == value)
{
hashtable[hfunction(value)] = hashtable[hfunction(value)]->urm;
}
else
{
node *first = hashtable[hfunction(value)];
while (first->urm->info != value)
{
first = first->urm;
}
node *deleted = first->urm;
first->urm = deleted->urm;
delete deleted;
}
}
int main()
{
fin >> n;
for (i = 1; i <= n; i++)
{
fin >> digit >> value;
if (digit == 1)
{
if (searchnode(value) == 0)
{
addnode(value);
}
}
if (digit == 2)
{
if (searchnode(value) == 1)
{
deletenode(value);
}
}
if (digit == 3)
{
fout << searchnode(value) << "\n";
}
}
fin.close();
fout.close();
return 0;
}