Advertisement
Guest User

Untitled

a guest
May 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. TValue Map::remove(TKey c)
  2. {
  3. //removes a key from the map and returns the value associated with the key if
  4. //the key existed ot null: NULL_TVALUE otherwise
  5. TValue searchResult = search(c);
  6. if (searchResult == NULL_TVALUE || c == NULL_TVALUE)
  7. return NULL_TVALUE;
  8.  
  9. int hashPosition = this->h(c);
  10.  
  11. if (this->T[hashPosition] == c)
  12. {
  13. TValue valueRemoved = this->values[hashPosition];
  14. this->T[hashPosition] = NULL_TVALUE;
  15. this->values[hashPosition] = NULL_TVALUE;
  16. return valueRemoved;
  17. }
  18. if (this->next[hashPosition] == -1)
  19. return NULL_TVALUE;
  20.  
  21. int nextPosition = this->next[hashPosition];
  22.  
  23. while (this->T[nextPosition] != c && this->next[nextPosition] != NULL_TVALUE)
  24. {
  25. hashPosition = nextPosition;
  26. nextPosition = this->next[nextPosition];
  27. }
  28.  
  29. //we check if we found it:
  30.  
  31. if (this->T[nextPosition] == c)
  32. {
  33. this->next[hashPosition] = this->next[nextPosition];
  34. TValue oldElement = this->values[nextPosition];
  35. this->values[nextPosition] = NULL_TVALUE;
  36. return oldElement;
  37.  
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement