Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TValue Map::remove(TKey c)
- {
- //removes a key from the map and returns the value associated with the key if
- //the key existed ot null: NULL_TVALUE otherwise
- TValue searchResult = search(c);
- if (searchResult == NULL_TVALUE || c == NULL_TVALUE)
- return NULL_TVALUE;
- int hashPosition = this->h(c);
- if (this->T[hashPosition] == c)
- {
- TValue valueRemoved = this->values[hashPosition];
- this->T[hashPosition] = NULL_TVALUE;
- this->values[hashPosition] = NULL_TVALUE;
- return valueRemoved;
- }
- if (this->next[hashPosition] == -1)
- return NULL_TVALUE;
- int nextPosition = this->next[hashPosition];
- while (this->T[nextPosition] != c && this->next[nextPosition] != NULL_TVALUE)
- {
- hashPosition = nextPosition;
- nextPosition = this->next[nextPosition];
- }
- //we check if we found it:
- if (this->T[nextPosition] == c)
- {
- this->next[hashPosition] = this->next[nextPosition];
- TValue oldElement = this->values[nextPosition];
- this->values[nextPosition] = NULL_TVALUE;
- return oldElement;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement