Pagini recente » Cod sursa (job #230024) | Cod sursa (job #1814149) | Cod sursa (job #1393078) | Cod sursa (job #1466539) | Cod sursa (job #1477586)
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#define MAX 1000003
using namespace std;
typedef struct list{
int info;
list *next;
} Cell, *List;
typedef struct{
List *lists;
} Hash;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
int N;
Hash hash;
int hashFunction(int x){
return x % MAX;
}
List *CheckintoHash(List *aL, int x){
if((*aL)->next == NULL)
return NULL;
List *r = NULL;
while((*aL) != NULL){
r = aL;
if((*aL)->info == x)
break;
aL = &(*aL)->next;
}
return r;
}
void AddtoHash(List *aL, int x){
List *r = CheckintoHash(aL, x);
if(r == NULL || (*r)->info != x){
List aux = (List)malloc(sizeof(Cell));
aux->info = x;
aux->next = NULL;
aux->next = (*aL)->next;
(*aL)->next = aux;
}
}
void RemovefromHash(List *aL, int x){
List *r = CheckintoHash(aL, x);
if(r != NULL && (*r)->info == x){
List aux = *r;
(*r) = (*r)->next;
free(aux);
}
}
void createHash(){
in >> N;
hash.lists = (List *)calloc((MAX+1), sizeof(List));
for(int i = 0; i < MAX+1; ++i){
hash.lists[i] = (List)malloc(sizeof(Cell));
List *aux = &hash.lists[i];
(*aux)->info = 0;
(*aux)->next = NULL;
}
int op, x;
for(int i = 0; i < N; ++i){
in >> op >> x;
int h = hashFunction(x);
switch(op){
case(1):
AddtoHash(&hash.lists[h], x);
break;
case(2):
RemovefromHash(&hash.lists[h], x);
break;
case(3):
List *r = CheckintoHash(&hash.lists[h], x);
if(r != NULL && (*r)->info == x)
out << 1 << "\n";
else
out << 0 << "\n";
break;
}
}
}
void deleteHash(){
for(int i = 0; i < MAX+1; ++i){
List *aL = &hash.lists[i];
while((*aL) != NULL){
List aux = *aL;
*aL = (*aL)->next;
free(aux);
}
free(*aL);
}
free(hash.lists);
}
int main(){
createHash();
//deleteHash();
return 0;
}