Mastering-Go-Second-Edition/ch05/hashTableLookup.go
2019-01-10 22:09:57 +02:00

69 lines
1.1 KiB
Go

package main
import (
"fmt"
)
const SIZE = 15
type Node struct {
Value int
Next *Node
}
type HashTable struct {
Table map[int]*Node
Size int
}
func hashFunction(i, size int) int {
return (i % size)
}
func insert(hash *HashTable, value int) int {
index := hashFunction(value, hash.Size)
element := Node{Value: value, Next: hash.Table[index]}
hash.Table[index] = &element
return index
}
func traverse(hash *HashTable) {
for k := range hash.Table {
if hash.Table[k] != nil {
t := hash.Table[k]
for t != nil {
fmt.Printf("%d -> ", t.Value)
t = t.Next
}
fmt.Println()
}
}
}
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
}
func main() {
table := make(map[int]*Node, SIZE)
hash := &HashTable{Table: table, Size: SIZE}
for i := 0; i < 120; i++ {
insert(hash, i)
}
for i := 10; i < 125; i++ {
if !lookup(hash, i) {
fmt.Println(i, "is not in the hash table!")
}
}
}