Cod sursa(job #1324903)

Utilizator deea101Andreea deea101 Data 22 ianuarie 2015 21:57:35
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
using namespace std;
#define NULL 0
#define NMAX 666013
class List
{
    private:
        struct node
        {
            int key;
            node *next;
        }*first;
    public:
        List()
        {
            first=NULL;
        }
        bool lookup(int x)
        {
			node *p=first;
            while(p!=NULL && p->key!=x)
                p=p->next;

            if(p==NULL) return 0;
            else return 1;
        }
        void push(int x)
        {    
            if(lookup(x)) return;

            node *p;
            p=new node;
            p->key=x;
            p->next=first;

            first=p;
        }
        void del(int x)
        {
            node *p=first;
            while(p!=NULL && p->key!=x)
                p=p->next;

            if(p==NULL) return;
            p->key=first->key;

            p=first;
            first=first->next;
            delete p;
        }
};
class Hash
{
    private:
        List v[NMAX];
		int size;

	int hash(int x)
	{
		return (x%size);
	}
    public:
        Hash()
        {
            size=NMAX;
        }
        bool lookup(int x)
        {
            int h=hash(x);
            return v[h].lookup(x);
        }
        void insert(int x)
        {
            int h=hash(x);
            v[h].push(x);
        }
        void del(int x)
        {
            int h=hash(x);
            v[h].del(x);
        }
}H;

#include <fstream>
ifstream f("hashuri.in");
ofstream g("hashuri.out");
int main()
{
    int T;
    f>>T;
     
    int q,x;
    while(T--)
    {
        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;
        }
    }
}