问题描述 给定散列表的数据类型如下 typedef int ElementType; typedef struct HashNode { ElementType key; struct HashNode * next; }HashNode,* List; typedef struct { int size; List * lists;//槽指针,所有链表均有头结点 }HashTable; 请设计HashNode * find(HashTable * h,ElementType key) 该函数在散列表中查找关键字key,并返回该结点指针;如果找不到则返回NULL。 以下函数已经定义,你可以直接调用 int hash(HashTable * h,ElementType key); 请注意,本题有预置代码,只需提交所要求的函数定义代码即可。 预置代码 #include <bits/stdc++.h> using namespace std; typedef int ElementType; typedef struct HashNode { ElementType key; struct HashNode * next; }HashNode,* List; typedef struct { int size; List * lists;//槽指针,所有链表均有头结点 }HashTable; HashNode * find(HashTable * h,ElementType key); int main() { /*此处代码由测试程序自动添加,主要为了向顺序表中插入数据 并输出数据,你无需关心此处代码的具体实现细节。 如果有必要,请自己添加代码以测试你的函数是否正确。 */ return 0; } /*你的提交的代码将被添加在此处,请完成题目所要求的函数的定义*/c代码
时间: 2024-02-06 19:02:18 浏览: 28
下面是我根据题目需求编写的代码:
```c++
HashNode * find(HashTable * h, ElementType key) {
int index = hash(h, key);
HashNode * node = h->lists[index]->next;
while (node != nullptr) {
if (node->key == key) {
return node;
}
node = node->next;
}
return nullptr;
}
```
首先,我们需要根据关键字 `key` 计算出它所对应的槽下标 `index`,这可以通过调用 `hash` 函数来实现。然后,我们需要遍历该槽中的链表,查找是否存在关键字 `key`。如果找到了,我们就可以返回该结点指针。如果遍历完链表还没有找到,我们就可以返回 `nullptr`,表示找不到该关键字。
需要注意的是,由于该散列表中的每个链表都有头结点,因此我们需要从头结点的下一个结点开始遍历链表。
相关问题
给定散列表的数据类型如下 typedef int ElementType; typedef struct HashNode { ElementType key; struct HashNode * next; }HashNode,* List; typedef struct { int size; List * lists;//槽指针,所有链表均有头结点 }HashTable; 请设计int hash(HashTable * h,ElementType key) 该函数计算关键字key的散列值,本题中散列值简单的取为key对散列表容量的余数。 请注意,本题有预置代码,只需提交所要求的函数定义代码即可。 c代码
好的,根据题目要求,我可以为你设计一个计算关键字key散列值的函数hash:
```c
int hash(HashTable *h, ElementType key) {
return key % h->size;
}
```
该函数接收一个 HashTable 指针和一个 ElementType 类型的关键字 key,返回 key 对散列表容量的余数作为散列值。
问题描述 给定散列表的数据类型如下 typedef int ElementType; typedef struct HashNode { ElementType key; struct HashNode * next; }HashNode,* List; typedef struct { int size; List * lists;//槽指针,所有链表均有头结点 }HashTable; 请设计HashNode * findPre(HashTable * h,ElementType key) 该函数在散列表查找关键字key所在节点的前驱并返回其指针,如果不存在则返回NULL指针。 以下函数已经定义,你可以直接调用 int hash(HashTable * h,ElementType key); 请注意,本题有预置代码,只需提交所要求的函数定义代码即可。 预置代码 #include <bits/stdc++.h> using namespace std; typedef int ElementType; typedef struct HashNode { ElementType key; struct HashNode * next; }HashNode,* List; typedef struct { int size; List * lists;//槽指针,所有链表均有头结点 }HashTable; HashNode * findPre(HashTable * h,ElementType key); int main() { /*此处代码由测试程序自动添加,主要为了向顺序表中插入数据 并输出数据,你无需关心此处代码的具体实现细节。 如果有必要,请自己添加代码以测试你的函数是否正确。 */ return 0; } /*你的提交的代码将被添加在此处,请完成题目所要求的函数的定义*/c代码
好的,根据题目要求,我可以为你设计一个查找关键字 key 节点前驱的函数 findPre:
```c
HashNode *findPre(HashTable *h, ElementType key) {
int index = hash(h, key); // 计算散列值
HashNode *pre = h->lists[index]; // 初始前驱节点为头结点
while (pre->next != NULL && pre->next->key != key) { // 遍历散列表链表,查找前驱节点
pre = pre->next;
}
if (pre->next == NULL) { // 如果关键字不存在,则返回 NULL
return NULL;
} else { // 如果关键字存在,则返回前驱节点指针
return pre;
}
}
```
该函数接收一个 HashTable 指针和一个 ElementType 类型的关键字 key,使用 `hash()` 函数计算关键字的散列值,然后遍历散列表中对应的链表,查找关键字所在节点的前驱节点并返回其指针。如果关键字不存在,则返回 NULL 指针。需要注意的是,所有链表均有头结点,所以初始前驱节点为头结点。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)