Advertisement
WONGDEEM

Untitled

Apr 2nd, 2021
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. int HashInsert(int key, HashSlot hashTable[])
  2. {
  3.   int index = hash(key), prevIndex = -1, counter = 0;
  4.  
  5.   if (hashTable[index].indicator == EMPTY) {
  6.     hashTable[index].key = key;
  7.     hashTable[index].indicator = USED;
  8.     hashTable[index].next = -1;
  9.     return index;
  10.   } else {
  11.    
  12.     if (HashFind(key,hashTable) != -1) return -1;
  13.    
  14.     while (counter < TABLESIZE) {
  15.       counter++;
  16.      
  17.       if (hashTable[index].next == -1) {
  18.         if (prevIndex == -1) prevIndex = index;
  19.         index = hash(index+1);
  20.       }
  21.      
  22.       if (hashTable[index].next != -1) {
  23.         index = hashTable[index].next;
  24.       }
  25.      
  26.       if (hashTable[index].indicator == EMPTY) {
  27.         hashTable[prevIndex].next = index;
  28.         hashTable[index].key = key;
  29.         hashTable[index].indicator = USED;
  30.         hashTable[index].next = -1;
  31.         return index;
  32.       }
  33.     }
  34.   }
  35.   return counter;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement