Pagini recente » Cod sursa (job #1446536) | Cod sursa (job #645859) | Cod sursa (job #2812532) | Cod sursa (job #2290015) | Cod sursa (job #2613876)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
class Node{
public:
int value;
int fr;
Node * prev;
Node * next;
Node(int val){
value = val;
fr = 1;
prev = NULL;
next = NULL;
}
};
class List{
public:
Node * start;
Node * end;
List(){
start = NULL;
end = NULL;
}
void add(int val){
Node * aux = start;
while(aux != NULL){
if(aux -> value == val){
aux -> fr += 1;
return;
}
aux = aux -> next;
}
Node * to_add = new Node(val);
if(start == NULL){
start = to_add;
}
else{
end -> next = to_add;
to_add -> prev = end;
}
end = to_add;
}
void del(Node * to_del){
if(to_del == NULL)
return;
if(to_del -> fr > 1){
to_del -> fr -= 1;
return;
}
if(start == to_del && to_del == end){
start = NULL;
}
else if(start == to_del){
start = start -> next;
start -> prev = NULL;
}
else if(end == to_del){
end = end -> prev;
end -> next = NULL;
}
else{
to_del -> prev -> next = to_del -> next;
to_del -> next -> prev = to_del -> prev;
}
delete to_del;
}
Node * search(int to_srch){
Node * aux = start;
while(aux != NULL){
if(aux -> value == to_srch)
return aux;
aux = aux -> next;
}
return NULL;
}
void print(){
Node * aux = start;
while(aux != NULL){
cout<<aux -> value<<":"<<aux -> fr<<" ";
aux = aux -> next;
}
cout<<'\n';
}
};
class Hashmap{
public:
int n;
List * hashValue;
Hashmap(int k){
n = k;
hashValue = new List[n];
}
bool search(int val){
int hval = val % n;
if(hashValue[hval].search(val) != NULL)
return 1;
return 0;
}
bool add(int val){
int hval = val % n;
hashValue[hval].add(val);
}
bool remove(int val){
int hval = val % n;
hashValue[hval].del(hashValue[hval].search(val));
}
};
int main(){
int n;
f>>n;
Hashmap h(n);
int q, x;
for(int i = 1; i <= n; i++){
f>>q>>x;
if(q == 1)
h.add(x);
else if(q == 2)
h.remove(x);
else
g<<h.search(x)<<'\n';
}
return 0;
}