Pagini recente » Cod sursa (job #2596099) | Cod sursa (job #904799) | Cod sursa (job #869829) | Cod sursa (job #2443052) | Cod sursa (job #641358)
Cod sursa(job #641358)
#include <stdio.h>
using namespace std;
class Node
{
Node *next;
int value;
public:
Node(int x, Node *element)
{
value = x;
next = element;
}
int getValue()
{
return value;
}
Node* getNext()
{
return next;
}
void setNext(Node* element)
{
next = element;
}
};
class SingleLinkedList
{
public:
Node *first;
Node *iterator;
SingleLinkedList()
{
first = NULL;
}
void initializeIterator()
{
iterator = first;
}
int getNext()
{
int v = iterator->getValue();
iterator = iterator->getNext();
return v;
}
int isEmpty()
{
return iterator == NULL;
}
int hasValue(int value)
{
for (Node *crt = first; crt != NULL; crt = crt->getNext())
{
if (crt->getValue() == value)
{
return 1;
}
}
return 0;
}
int erase(int value)
{
if (first->getValue() == value)
{
Node *crt = first;
first = first->getNext();
delete crt;
return 1;
}
for (Node *crt = first, *prev; crt != NULL; prev = crt, crt = crt->getNext())
{
if (crt->getValue() == value)
{
prev->setNext(crt->getNext());
delete crt;
return 1;
}
}
return 0;
}
void insert(int value)
{
Node *node = new Node(value, first);
first = node;
}
~SingleLinkedList()
{
Node *crt = first, *prev;
while( crt != NULL)
{
prev = crt;
crt = crt->getNext();
delete prev;
}
}
};
#define MOD 666013
class Hash
{
SingleLinkedList *L[MOD];
public:
Hash()
{
}
void insert(int value)
{
int hashKey = value % MOD;
if (L[hashKey] == NULL)
{
L[hashKey] = new SingleLinkedList();
}
if(!L[hashKey]->hasValue(value))
{
L[hashKey]->insert(value);
}
}
void erase(int value)
{
int hashKey = value % MOD;
if (L[hashKey] != NULL)
{
L[hashKey]->erase(value);
}
}
int find(int value)
{
int hashKey = value % MOD;
if (L[hashKey] != NULL)
{
return L[hashKey]->hasValue(value);
}
return 0;
}
};
int main()
{
freopen("hashuri.in", "r", stdin);
freopen("hashuri.out", "w", stdout);
int n, x, y;
Hash *h = new Hash();
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
scanf("%d %d\n", &x, &y);
if (x == 1)
{
h->insert(y);
}
else if (x == 2)
{
h->erase(y);
}
else
{
printf("%d\n", h->find(y));
}
}
return 0;
}