Cod sursa(job #2616614)

Utilizator bogdan.gusuleacGusuleac Bogdan bogdan.gusuleac Data 19 mai 2020 14:44:29
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.42 kb
#include<iostream>
#include<fstream>
using namespace std;

fstream fin("hashuri.in", ios::in);
fstream fout("hashuri.out", ios::out);

#define prime 99991
///cel mai apropiat numar de radicalul numarului prim 44729

class Node
{
private:
    unsigned int info;
    Node *next;

public:

    Node(int info)
    {
        this->info = info;
        this->next = NULL;
    }

    Node()
    {
        this->info = -1;
        this->next = NULL;
    }

    void set_info(int info)
    {
        this->info = info;
    }

    void set_next(Node *node)
    {
        this->next = node;
    }

    int get_info()
    {
        return info;
    }

    Node *get_next()
    {
        return next;
    }

};

class HashStructure
{
protected:
    Node *data[prime];
public:
    HashStructure()
    {
        for(int i = 0; i < prime; i++)
            data[i] = 0;
    }

    void addElement(unsigned int a)
    {
        unsigned int pos = a % prime;
        Node *temp = new Node(a);

        if(data[pos] == NULL)
        {
            data[pos] = temp;
        }
        else
        {
            Node *cur = data[pos];

            while(cur->get_next() != NULL)
                cur = cur->get_next();

            cur->set_next(temp);
        }
    }

    bool searchElement(int a)
    {
        Node *result = searchElement(data[a % prime], a);

        if(result == NULL)
            return false;
        else
            return true;
    }

    Node* searchElement(Node *head, int a)
    {
        if (head == NULL)
            return NULL;

        if (head->get_info() == a)
            return head;

        if(head->get_next() == NULL)
            return NULL;
        else
            return searchElement(head->get_next(), a);
    }

    void deleteElement(unsigned int a)
    {
        int pos = a % prime;

        if(data[pos] == NULL)
            return;

        if(data[pos]->get_info() == a)
            data[pos] = data[pos]->get_next();
        else
            deleteElement(data[pos], a);
    }

    void deleteElement(Node *head, unsigned int a)
    {
        if(head->get_next() != NULL)
            if(head->get_next()->get_info() == a)
            {
                Node *next = head->get_next()->get_next();
                delete head->get_next();
                head->set_next(next);
            }
    }

    void printList()
    {
        for(int i = 0; i < prime; i++)
            if(data[i] != NULL)
                cout<<data[i]<<" ";

        cout<<"\n";
    }

    friend istream& operator >>(istream &input, HashStructure &h)
    {
        int a; input>>a;
        h.addElement(a);
        return input;
    }
};

int main()
{
    HashStructure hashOne;
    int  n, op, value;

    fin>>n;
    //cout<<n<<"\n";

    while(n--)
    {
        fin>>op>>value;
        //cout<<op<<" "<<value<<"\n";

        if(op == 1)
            hashOne.addElement(value);
        if(op == 2)
            hashOne.deleteElement(value);
        if(op == 3)
            fout<<hashOne.searchElement(value)<<"\n";
    }

    /*hashOne.addElement(10);
    hashOne.addElement(12);
    hashOne.addElement(21);
    hashOne.addElement(44739);
    hashOne.printList();
    hashOne.deleteElement(44739);
    hashOne.printList();*/
    //cout<<"dadada "<<int(hashOne.searchElement(44739))<<"\n";
    //hashOne.printList();
}