Cod sursa(job #1301471)

Utilizator deea101Andreea deea101 Data 25 decembrie 2014 23:34:02
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.1 kb
#include <fstream>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");

const int size=524228;

class LinkedList
{
    private:
        struct node
        {
            int key;
            node *next;
        }; 
        node *first,*last;

    public:
        LinkedList()
        {
            first=last=NULL;
        }

        bool isIn(int key)
        {
            node *p=first;
            while(p!=NULL && p->key!=key) p=p->next;

            if(p==NULL) return 0;
            else return 1;
        }

        void push_back(int key)
        {
            node *p;
            p=new node;
            p->next=NULL;
            p->key=key;

            if(first==NULL) first=last=p;
            else 
            {
                last->next=p;
                last=p;
            }
        }
        
        void remove(int key)
        {
            node *p=first;
            while(p!=NULL && p->key!=key) p=p->next;
            
            if(p==NULL) return;
            else
            {
                p=first;
				if(first==last) first=last=NULL;
                else first=first->next;
            }
        }
};
                
            
class HashTable
{
    private:
        LinkedList v[size];

        int hash(int key)
        {
            return (key%size);
        }

    public:
        
        bool query(int key)
        {
            int h=hash(key);
            if(v[h].isIn(key)) return 1;
                else return 0;
        }
        void insert(int key)
        {
            int h=hash(key);
            if(!v[h].isIn(key)) v[h].push_back(key);
        }
        void remove(int key)
        {
            int h=hash(key);
            v[h].remove(key);
        }
}H;

    int main()
    {
        int T,q,x;
        f>>T;
        while(T--)
        {
            f>>q>>x;
            switch (q)
            {
                case 1: H.insert(x); break;
                case 2: H.remove(x); break;
                case 3: g<<H.query(x)<<'\n';
            }
        }
    }