Cod sursa(job #1308941)

Utilizator deea101Andreea deea101 Data 4 ianuarie 2015 21:49:09
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.75 kb
#define NMAX 666013
#define NULL 0

using namespace std;

class List
{
    private:    
        struct node
        {
            int key;
            node *next;
            node()
            {
                next=NULL;
            }
        }*first;
    public:
        List()
        {
            first=NULL;
        }
        void insert(int key)
        {
            if(lookup(key)) return;

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

            if(p==NULL) return 0;
			else return 1;
		}
		void del(int key)
		{
			node *p=first;
			while(p!=NULL && p->key!=key) p=p->next;
			if(p==NULL) return;
			
			p->key=first->key;
			p=first;
			first=first->next;
			delete p;
		}
}; 
class Hash
{
    private:
        List L[NMAX];    
        int size;
        int hash(int key)
        {
            return key%size;
        }
    
    public:
		Hash()
		{
			size=NMAX;
		}
        void insert(int key)
        {
            int h=hash(key);
            L[h].insert(key);
        }
        bool lookup(int key)
        {
            int h=hash(key);
            return L[h].lookup(key);
        }
        void del(int key)
        {
            int h=hash(key);
            return L[h].del(key);
        }
}H;

#include <fstream>

ifstream f("hashuri.in");
ofstream g("hashuri.out");
int main()
{
	int N,q,x;
	f>>N;
	while(N--)
	{
		f>>q>>x;
		switch(q)
		{
			case 1:H.insert(x); break;
			case 2:H.del(x); break;
			case 3:g<<H.lookup(x)<<'\n'; break;
		}
	}
}