Skip to content

Commit

Permalink
add hashtable delete function
Browse files Browse the repository at this point in the history
  • Loading branch information
priyankchheda committed Mar 12, 2019
1 parent abbabe6 commit a0ff9f9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions hashtable/hashtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,40 @@ func (h *HashTable) Get(key int) (bool, int) {
return false, 0
}

func (h *HashTable) del(key int) bool {
index := hashFunction(key, len(h.Hash))
iterator := h.Hash[index]
if iterator == nil {
return false
}
if iterator.Key == key {
h.Hash[index] = iterator.Next
h.Size -= 1
return true
} else {
prev := iterator
iterator = iterator.Next
for iterator != nil {
if iterator.Key == key {
prev.Next = iterator.Next
h.Size -= 1
return true
}
prev = iterator
iterator = iterator.Next
}
return false
}
}

func (h *HashTable) Del(key int) bool {
sizeChanged := h.del(key)
if sizeChanged == true {
h.checkLoadFactorAndUpdate()
}
return sizeChanged
}

func (h *HashTable) getLoadFactor() float64 {
return float64(h.Size) / float64(len(h.Hash))
}
Expand Down

0 comments on commit a0ff9f9

Please sign in to comment.