Jirachi2016
Eternal Poster
- Joined
- Feb 20, 2016
- Posts
- 739
- Reaction
- 190
- Points
- 269
- Age
- 23
Mga boss sana may makatulong sa akin diko alam kung paano toh
Topic po ay sa Hashing
Linear Probing Collision Resolution
#define LOAD_FACTOR 20
struct ListNode {
int key;
int data;
struct ListNode *next;
};
struct HashTableNode {
int bcount;
struct ListNode *next;
};
struct HashTable {
int tsize;
int count;
struct HashTableNode **Table;
};
struct HashTable *CreateHashTable(int size){
struct HashTable *h
h = (struct HashTable *)malloc(sizeof(struct HashTable));
if(!h)
return NULL;
h tsize = size/ LOAD_FACTOR;
h ount = 0;
h Table = (struct HashTableNode *)malloc(sizeof(struct HashTableNode *) h tsize);
if(! Table){
cout("Memory Error");
return NULL;
}
for(int i=0; i<h-size;i++){
h Table next = NULL;
h Table bcount = 0;
}
return h;
}
int HashSearch(struct HashTable *h, int data){
struct ListNode *temp;
temp = h Table[Hash(data, h tsize)] next;
while (temp){
if (temp-data == data)
return 1;
temp = temp next;
}
return 0;
}
int HashInsert(Struct HashTable *h, int data){
int index;
struct ListNode *temp, *newNode;
if(HashSearch(h,data))
return 0;
index = Hash(data, h- tsize);
newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
if(!newNode){
printf("Out of Space");
return -1;
}
newNode key = index;
newNode data = data;
newNode next = h Table[index] next;
}
h Table[index] next = newNode;
h Table[index] bcount++;
h count++;
if(h count / h size > LOAD_FACTOR)
Rehash(h);
return 1;
}
int HashDelete(Struct HashTable *h, int data){
int index;
struct ListNode *temp, *prev;
index = Hash(data, h tsize);
for(temp = h Table[index] next, prev = NULL; temp,temp = temp next){
if(temp data == data){
if(prev!=NULL)
prev next = temp next
free(temp);
h Table[index] bcount--;
h count--;
return 1;
}
}
return 0;
}
void Rehash(Struct HashTable *h){
int oldsize,i,index;
struct ListNode *p, *temp, *temp2;
struct HashTableNode **oldTable;
oldsize = h tsize;
oldtableNode **oldTable;
h tsize = h tsize * 2;
h Table = (struct HashTableNode **)malloc(h tsize * sizeof(struct HashTableNode *));
if(!h Table){
printf("Allocation Failed");
return;
}
for(i = 0; i < oldsize; i++){
index = Hash(temp; temp data, h tsize);
temp2 = temp;temp = temp next;
temp2 next = h Table[index] next;
h TAble[index] next = temp2;
}
}
Topic po ay sa Hashing
Linear Probing Collision Resolution
#define LOAD_FACTOR 20
struct ListNode {
int key;
int data;
struct ListNode *next;
};
struct HashTableNode {
int bcount;
struct ListNode *next;
};
struct HashTable {
int tsize;
int count;
struct HashTableNode **Table;
};
struct HashTable *CreateHashTable(int size){
struct HashTable *h
h = (struct HashTable *)malloc(sizeof(struct HashTable));
if(!h)
return NULL;
h tsize = size/ LOAD_FACTOR;
h ount = 0;
h Table = (struct HashTableNode *)malloc(sizeof(struct HashTableNode *) h tsize);
if(! Table){
cout("Memory Error");
return NULL;
}
for(int i=0; i<h-size;i++){
h Table next = NULL;
h Table bcount = 0;
}
return h;
}
int HashSearch(struct HashTable *h, int data){
struct ListNode *temp;
temp = h Table[Hash(data, h tsize)] next;
while (temp){
if (temp-data == data)
return 1;
temp = temp next;
}
return 0;
}
int HashInsert(Struct HashTable *h, int data){
int index;
struct ListNode *temp, *newNode;
if(HashSearch(h,data))
return 0;
index = Hash(data, h- tsize);
newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
if(!newNode){
printf("Out of Space");
return -1;
}
newNode key = index;
newNode data = data;
newNode next = h Table[index] next;
}
h Table[index] next = newNode;
h Table[index] bcount++;
h count++;
if(h count / h size > LOAD_FACTOR)
Rehash(h);
return 1;
}
int HashDelete(Struct HashTable *h, int data){
int index;
struct ListNode *temp, *prev;
index = Hash(data, h tsize);
for(temp = h Table[index] next, prev = NULL; temp,temp = temp next){
if(temp data == data){
if(prev!=NULL)
prev next = temp next
free(temp);
h Table[index] bcount--;
h count--;
return 1;
}
}
return 0;
}
void Rehash(Struct HashTable *h){
int oldsize,i,index;
struct ListNode *p, *temp, *temp2;
struct HashTableNode **oldTable;
oldsize = h tsize;
oldtableNode **oldTable;
h tsize = h tsize * 2;
h Table = (struct HashTableNode **)malloc(h tsize * sizeof(struct HashTableNode *));
if(!h Table){
printf("Allocation Failed");
return;
}
for(i = 0; i < oldsize; i++){
index = Hash(temp; temp data, h tsize);
temp2 = temp;temp = temp next;
temp2 next = h Table[index] next;
h TAble[index] next = temp2;
}
}