-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.kt
91 lines (80 loc) · 2.29 KB
/
HashTable.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.ds.implementation.hashmap
import kotlin.math.abs
class HashTable<K, V>(private val size: Int) : HashTableOperations<K,V>{
private val table = Array<HashNode<K, V>?>(size) { null }
private fun createKey(key: K): Int = abs(key.hashCode() % size)
override fun put(key: K, value: V) {
val hashNode = HashNode(key, value)
val hashKey = createKey(key)
if(table[hashKey] == null)
{
table[hashKey] = hashNode
} else {
var head = table[hashKey]
while (head?.next != null)
{
head = head.next
}
head?.next = hashNode
}
}
override fun remove(key: K) : Boolean {
val hashKey = createKey(key)
if(table[hashKey] == null) return false
else {
var head = table[hashKey]
var prev = head
while (head != null) {
if(head.key?.equals(key) == true)
{
prev?.next = head.next
return true
}
prev = head
head = head.next
}
}
return false
}
override fun get(key: K): V? {
val hashKey = createKey(key)
if(table[hashKey] == null) return null
else {
var head = table[hashKey]
while (head?.next != null) {
if(head.key?.equals(key) == true)
{
return head.value
}
head = head.next
}
}
return null
}
override fun print(msg: String?) {
msg?.let { println(it) }
table.forEachIndexed { index, hashNode ->
kotlin.io.print("HashKey $index Entries ->")
var head = hashNode
while (head != null)
{
kotlin.io.print("|${head.key}:${head.value}|->")
head = head.next
}
kotlin.io.print("X")
println()
}
}
}
fun main() {
//Name and Age
val table = HashTable<String, Int>(10)
table.put("John", 32)
table.put("Sam", 22)
table.put("Asm", 22)
table.put("Jimmy", 11)
table.put("Adam", 23)
table.print()
table.remove("Sam")
table.print("After removing Sam")
}