Cod sursa(job #641358)

Utilizator peanutzAndrei Homorodean peanutz Data 27 noiembrie 2011 23:54:42
Problema Hashuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 3.15 kb
#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;
}