August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this section, you are going to see an implantation of the lookup() function that allows you to determine whether a given element already exists in the hash table or not. The code of the lookup() function is based on that of the traverse() function, as follows:
func lookup(hash *HashTable, value int) bool {
index := hashFunction(value, hash.Size)
if hash.Table[index] != nil {
t := hash.Table[index]
for t != nil {
if t.Value == value {
return true
}
t = t.Next
}
}
return false
}
You can find the preceding code in the hashTableLookup.go source file. Executing hashTableLookup.go will create the following output:
$ go run hashTableLookup.go 120 is not in the hash table! 121 is not in the hash table!
Read now
Unlock full access