void OAHT_Set(HashTable** HT, KeyType Key, ValueType Value) { int KeyLen, Address, StepSize; double Usage; Usage = (double)(*HT)->OccupiedCount / (*HT)->TableSize; if (Usage > 0.5) { OAHT_Rehash(HT); } KeyLen = strlen(Key); Address = OAHT_Hash(Key, KeyLen, (*HT)->TableSize); StepSize = OAHT_Hash2(Key, KeyLen, (*HT)->TableSize); while ((*HT)->Table[Address].Status != EMPTY && strcmp((*HT)->Table[Address].Key, Key) != 0) { printf("Collision occured! : Key(%s), Address(%d), StepSize(%d) \n", Key, Address, StepSize); Address = (Address + StepSize) % (*HT)->TableSize; } (*HT)->Table[Address].Key = (char*)malloc(sizeof(char) * (KeyLen + 1)); strcpy((*HT)->Table[Address].Key, Key); (*HT)->Table[Address].Value = (char*)malloc(sizeof(char) * (strlen(Value) + 1)); strcpy((*HT)->Table[Address].Value, Value); (*HT)->Table[Address].Status = OCCUPIED; (*HT)->OccupiedCount++; printf("Key(%s) entered at address(%d) \n", Key, Address); }
ValueType OAHT_Get( HashTable* HT, KeyType Key ) { int KeyLen = strlen(Key); int Address = OAHT_Hash( Key, KeyLen, HT->TableSize ); int StepSize = OAHT_Hash2( Key, KeyLen, HT->TableSize ); while ( HT->Table[Address].Status != EMPTY && strcmp(HT->Table[Address].Key, Key) != 0 ) { Address = (Address + StepSize) % HT->TableSize; } return HT->Table[Address].Value; }