Cod sursa(job #1716967)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 14 iunie 2016 00:34:45
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.95 kb
#include <bits/stdc++.h>

using namespace std;

class LinkedList {
    private:
        struct Node {
            Node* next;
            int info;

            Node() {}
        };

    public:
        LinkedList() {
            pre = new Node();
            post = new Node();
            pre->next = post;
            post->next = nullptr;
        }

        ~LinkedList() {
            Node* current = pre;
            while (current != nullptr) {
                Node* aux = current;
                current = current->next;
                delete aux;
            }
        }

        LinkedList::Node* find(int value) {
            Node* prev = pre;
            Node* current = pre->next;

            while (current != post) {
                if (current->info == value) {
                    return prev;
                }

                prev = current;
                current = current->next;
            }

            return nullptr;
        }

        void append(int value) {
            Node* current = find(value);

            if (current == nullptr) {
                current = new Node();
                current->info = value;
                Node* aux = pre->next;
                pre->next = current;
                current->next = aux;
            }
        }

        void remove(int value) {
            Node* prev = find(value);

            if (prev != nullptr) {
                Node* current = prev->next;
                prev->next = current->next;
                delete current;
            }
        }

    private:
        friend class HashTable;
        Node* pre;
        Node* post;
};

class HashTable {
    public:
        HashTable() {
            for (int i = 0; i < MOD; i++) {
                hash[i] = new LinkedList();
            }
        }

        ~HashTable() {
            for (int i = 0; i < MOD; i++) {
                delete hash[i];
            }
        }

        static int hash_function(int value) {
            return value % MOD;
        }

        void add(int value) {
            int key = hash_function(value);
            hash[key]->append(value);
        }

        void remove(int value) {
            int key = hash_function(value);
            hash[key]->remove(value);
        }

        bool contains(int value) {
            int key = hash_function(value);
            LinkedList::Node* node = hash[key]->find(value);
            return node != nullptr;
        }

    private:
        static const int MOD = 666013;
        LinkedList* hash[MOD];
};

int main() {
    cin.sync_with_stdio(false);

    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    HashTable my_hash;

    int n;
    scanf("%d", &n);

    for (; n; n--) {
        int op, value;
        scanf("%d%d", &op, &value);

        if (op == 1) {
            my_hash.add(value);
        } else if (op == 2) {
            my_hash.remove(value);
        } else {
            printf("%d\n", my_hash.contains(value));
        }
    }

    return 0;
}