#C9439. Implement a Hash Map using Separate Chaining
Implement a Hash Map using Separate Chaining
Implement a Hash Map using Separate Chaining
You are required to implement a hash map from scratch using separate chaining to handle collisions. The hash map should support the following operations:
put(key, value)
: Insert a key-value pair into the hash map. If the key already exists, update its value.get(key)
: Return the value associated with the key. If the key does not exist, return-1
.remove(key)
: Remove the key and its corresponding value from the hash map. If the key does not exist, do nothing.
The hash function to be used is \(hash(key) = key \mod m\) where \(m = 1000\). Collision resolution should be implemented using separate chaining with linked lists.
This problem is designed to test your understanding of hash tables and handling collisions in data structures.
inputFormat
The input is read from stdin and is structured as follows:
- The first line contains an integer \(n\) representing the number of operations.
- The following \(n\) lines each contain an operation in one of the following forms:
put key value
: Insert or update the key with the given value.get key
: Retrieve the value associated with the key.remove key
: Remove the key from the hash map.
Note: Keys and values are integers. Only the get
operation produces output.
outputFormat
For each get
operation, output the corresponding value on a new line to stdout. If a key does not exist, output -1
.
5
put 1 10
put 2 20
get 1
remove 1
get 1
10
-1
</p>