THINK FIRST·CODE LATER

← All labs

Linear Probing Table

Problem

Read the table size m, the number of keys n, and then n integer keys. Insert the keys in order into an empty hash table of size m using h(k) = Math.floorMod(k, m) and linear probing (h(k), h(k)+1, … wrapping round to 0).

  • If a key is already in the table, print Duplicate: K and skip it.
  • If all m slots are examined without finding a free one, print Table full: K not inserted.

Then print every slot as [i] key, or [i] - for an empty slot, followed by the total number of collisions (occupied slots skipped while inserting the keys that were stored) and the load as size/m.

Input:

11 6 22 35 13 46 57 24

Output:

[0] 22
[1] -
[2] 35
[3] 13
[4] 46
[5] 57
[6] 24
[7] -
[8] -
[9] -
[10] -
Collisions: 10
Load: 6/11

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d14-1-data-1.txt

11 6 22 35 13 46 57 24
What your program must print for d14-1-data-1.txt
[0] 22
[1] -
[2] 35
[3] 13
[4] 46
[5] 57
[6] 24
[7] -
[8] -
[9] -
[10] -
Collisions: 10
Load: 6/11

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d14-1-data-1.txt

Windows PowerShell does not understand <:

Get-Content d14-1-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.