Build a hashtable using arrays instead of linked lists.
# Program to build a hashtable using arrays instead of linked lists.
h=Array.new #creating an empty array
arr0=Array.new
arr1=Array.new
arr2=Array.new
arr3=Array.new
arr4=Array.new
arr5=Array.new
arr6=Array.new
arr7=Array.new
arr8=Array.new
arr9=Array.new
h=[arr0,arr1,arr2,arr3,arr4,arr5,arr6,arr7,arr8,arr9]
for i in 1 ..10 do
print("Enter an integer that you want to hash and store:")
str=gets #The no. entered through the keyboard is stored in str as a string
str.chomp! #To remove the trailing newline
num=str.to_i #The string str is converted into an int
keyindex=num%10 #The key is obtained by performing the modulus of the given no.
case keyindex
when 0
arr0[arr0.size]=num
when 1
arr1[arr1.size]=num
when 2
arr2[arr2.size]=num
when 3
arr3[arr3.size]=num
when 4
arr4[arr4.size]=num
when 5
arr5[arr5.size]=num
when 6
arr6[arr6.size]=num
when 7
arr7[arr7.size]=num
when 8
arr8[arr8.size]=num
else
arr9[arr9.size]=num
end
end
#To print and see if its been stored like in a hashtable
#print("The values stored at arr0: #{arr0.find_all{|e| e % 1 == 0}}\n")
print("The values at h[0] are: #{h.values_at(0)}\n")
print("The values at h[1] are: #{h.values_at(1)}\n")
print("The values at h[2] are: #{h.values_at(2)}\n")
print("The values at h[3] are: #{h.values_at(3)}\n")
print("The values at h[4] are: #{h.values_at(4)}\n")
print("The values at h[5] are: #{h.values_at(5)}\n")
print("The values at h[6] are: #{h.values_at(6)}\n")
print("The values at h[7] are: #{h.values_at(7)}\n")
print("The values at h[8] are: #{h.values_at(8)}\n")
print("The values at h[9] are: #{h.values_at(9)}\n")