哈希表插入和删除元素

时间:2022-07-29 14:14:46

I'm trying to do a Hash table with linear probing insertion.

我正在尝试使用线性探测插入来执行哈希表。

The size of the table is 11, my hash function is, h(k) = k mod 11, and what i want to do is.

表的大小是11,我的哈希函数是,h(k)= k mod 11,我想要做的是。

Insert(15,c) Insert(4,a) Insert(26,b) Delete(15) Insert(5,d) Insert(4,e)

插入(15,c)插入(4,a)插入(26,b)删除(15)插入(5,d)插入(4,e)

Here is my solution but it aint right.

这是我的解决方案,但它没有权利。

哈希表插入和删除元素

It is supposed to be like this, can someone explain why?

它应该是这样的,有人能解释为什么吗?

哈希表插入和删除元素

2 个解决方案

#1


1  

Ahh, you clarified your question.

啊,你澄清了你的问题。

The answer is correct.

答案是对的。

Your solution is incorrect because when you do the insert(4,e), you're not first checking to see if that key already exists in the hash table. If it does exist, then you need to overwrite it.

您的解决方案不正确,因为当您执行insert(4,e)时,您不是首先检查哈希表中是否已存在该键。如果确实存在,则需要覆盖它。

#2


0  

Whe you insert(15,c) and (4,a), I believe you need create a linked list to solve the collision since they have the same key(4). Then Delete(15) will delete both a and c.

当你插入(15,c)和(4,a)时,我相信你需要创建一个链表来解决碰撞,因为它们有相同的键(4)。然后删除(15)将删除a和c。

#1


1  

Ahh, you clarified your question.

啊,你澄清了你的问题。

The answer is correct.

答案是对的。

Your solution is incorrect because when you do the insert(4,e), you're not first checking to see if that key already exists in the hash table. If it does exist, then you need to overwrite it.

您的解决方案不正确,因为当您执行insert(4,e)时,您不是首先检查哈希表中是否已存在该键。如果确实存在,则需要覆盖它。

#2


0  

Whe you insert(15,c) and (4,a), I believe you need create a linked list to solve the collision since they have the same key(4). Then Delete(15) will delete both a and c.

当你插入(15,c)和(4,a)时,我相信你需要创建一个链表来解决碰撞,因为它们有相同的键(4)。然后删除(15)将删除a和c。