Pagini recente » Cod sursa (job #2710176) | Cod sursa (job #2384214) | Cod sursa (job #1179925) | Cod sursa (job #1085192) | Cod sursa (job #1562952)
#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;
}