Advertisement
WONGDEEM

Untitled

Apr 2nd, 2021
626
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int HashInsert(int key, HashSlot hashTable[])
  2. {
  3.   int index, prevIndex = -1, counter = 0;
  4.  
  5.   index = hash(key);
  6.   if (hashTable[index].indicator == EMPTY) {
  7.     hashTable[index].key = key;
  8.     hashTable[index].indicator = USED;
  9.     hashTable[index].next = -1;
  10.     return index;
  11.   } else if (hashTable[index].indicator == USED) {
  12.     if (HashFind(key,hashTable) != -1) return -1;
  13.     while (counter < TABLESIZE) {
  14.       counter++;
  15.       if (hashTable[index].next == -1) {
  16.         if (prevIndex == -1) prevIndex = index;
  17.         index = hash(index+1);
  18.       }
  19.       if (hashTable[index].next != -1) {
  20.         index = hashTable[index].next;
  21.       }
  22.       if (hashTable[index].indicator == EMPTY) {
  23.         hashTable[prevIndex].next = index;
  24.         hashTable[index].key = key;
  25.         hashTable[index].indicator = USED;
  26.         hashTable[index].next = -1;
  27.         return index;
  28.       }
  29.     }
  30.   }
  31.   return counter;
  32. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement