Pagini recente » Cod sursa (job #2534486) | Cod sursa (job #548747) | Cod sursa (job #2299129) | Cod sursa (job #2639845) | Cod sursa (job #1850638)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define tableSize 499980
#define empty 0
#define MOD 499979
struct item{
int data;
struct item* next;
};
struct item* HashTable[tableSize];
void Initialize(){
int i;
for(i=0; i<tableSize; i++){
HashTable[i] = (struct item*)malloc(sizeof(struct item));
HashTable[i] -> data = empty;
HashTable[i] -> next = NULL;
}
}
int h(int x){
int h_x = x % MOD;
return (h_x) ? h_x : MOD;
}
void Insert(int x){
int index = h(x);
if(HashTable[index] -> data == empty){
HashTable[index] -> data = x;
}else{
struct item* ptr = HashTable[index];
struct item* newItem = (struct item*)malloc(sizeof(struct item));
newItem -> data = x;
newItem -> next = NULL;
while(1){
if(ptr -> data == x){
return;
}
if(ptr -> next == NULL){
break;
}
ptr = ptr -> next;
}
ptr -> next = newItem;
}
}
int Search(int x){
int index = h(x);
struct item* ptr = HashTable[index];
while(ptr != NULL){
if(ptr -> data == x){
return 1;
}
ptr = ptr -> next;
}
return 0;
}
void Delete(int x){
int index = h(x);
struct item* delPtr;
struct item* P1;
struct item* P2;
if(HashTable[index] -> data == empty){
return;
}
if(HashTable[index] -> data == x){
if(HashTable[index] -> next == NULL){
HashTable[index] -> data = empty;
return;
}else{
delPtr = HashTable[index];
HashTable[index] = HashTable[index] -> next;
free(delPtr);
return;
}
}
P1 = HashTable[index] -> next;
P2 = HashTable[index];
while(P1 != NULL && P1 -> data != x){
P2 = P1;
P1 = P1 -> next;
}
if(P1 == NULL){
return;
}
delPtr = P1;
P1 = P1 -> next;
P2 -> next = P1;
free(delPtr);
}
int main() {
Initialize();
freopen("hashuri.in", "r", stdin);
freopen("hashuri.out", "w", stdout);
int T, op, x;
scanf("%d", &T);
while(T--){
scanf("%d", &op);
scanf("%d", &x);
if(op==1){
Insert(x);
}else if(op==2){
Delete(x);
}else if(op==3){
printf("%d\n", Search(x));
}
}
return 0;
}