Cod sursa(job #1562952)

Utilizator NitaMihaitavoidcube NitaMihaita Data 5 ianuarie 2016 16:36:43
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.64 kb
#include <fstream>
using namespace std;

ifstream f("hashuri.in");
ofstream g("hashuri.out");

template <typename T>
struct nodLS {
	T value;
	struct nodLS<T> *next;
};

template <typename T>
struct SimpleLinkedList {
	nodLS<T> *first = NULL;

	void Add(T x) {
		nodLS<T> *p = new nodLS<T>;
		p->value = x;
		p->next = first;
		first = p;
	}

	bool Find(T x) {
		nodLS<T> *p;
		for(p = first; p; p = p->next)
			if(p->value == x) return true;
		return false;
	}

	bool Delete(T x) {
		bool found = false;
		nodLS<T> *pb = NULL, *p;
		for(p = first; p; p = p->next) {
			if(p->value == x) {
				found = true;
				break;
			}
			pb = p;
		}

		if(found) {
			if(!pb) first = first->next;
			else pb->next = p->next;
			delete p;
			return true;
		}

		return false;

	}
};

template <typename T>
struct Hash {
    int capacity;
    int (*hashFunction)(T );
    SimpleLinkedList<T> *v;

    Hash(int c=0, int (*func)(T)=NULL){
        hashFunction=func;
        capacity=c;
        if(capacity>0) v=new SimpleLinkedList<T>[capacity];
    }

    void Insert(T x){
        v[hashFunction(x)].Add(x);
    }

    bool Find(T x){
        return v[hashFunction(x)].Find(x);
    }

    bool Delete(T x){
        return v[hashFunction(x)].Delete(x);
    }
};

int HashFunction665993(int x){
    return x%665993;
}

int main(){
    Hash<int> ha(665993,HashFunction665993);
    int n,op,x;
    f>>n;
    while(n--){
        f>>op>>x;
        if(op==1) ha.Insert(x);
        else if(op==2) ha.Delete(x);
        else g<<(int)ha.Find(x)<<"\n";
    }

    f.close();
    g.close();
    return 0;
}