请用C语言写一个HashTable的示例
时间: 2024-05-09 14:17:47 浏览: 13
当然,我可以为您编写一个简单的 HashTable 示例程序,实现基本的哈希表功能。以下是一个基本的实现示例:
```
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
struct Node {
int data;
int key;
struct Node* next;
};
struct Node* hashTable[SIZE];
void insert(int key, int data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->key = key;
new_node->data = data;
new_node->next = NULL;
int hash_index = key % SIZE;
if (hashTable[hash_index] == NULL){
hashTable[hash_index] = new_node;
}else {
struct Node* tmp = hashTable[hash_index];
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = new_node;
}
}
void delete(int key) {
int hash_index = key % SIZE;
struct Node* head = hashTable[hash_index];
struct Node* tmp = head;
struct Node* prev = NULL;
if (head == NULL) {
printf("Key not found\n");
return;
}
while (tmp->next != NULL && tmp->key != key) {
prev = tmp;
tmp = tmp->next;
}
if (tmp->key == key) {
if (prev == NULL) {
hashTable[hash_index] = tmp->next;
} else {
prev->next = tmp->next;
}
free(tmp);
printf("Key deleted\n");
} else {
printf("Key not found\n");
}
}
void search(int key) {
int hash_index = key % SIZE;
struct Node* head = hashTable[hash_index];
struct Node* tmp = head;
if (head == NULL) {
printf("Key not found\n");
return;
}
while (tmp != NULL && tmp->key != key) {
tmp = tmp->next;
}
if (tmp == NULL) {
printf("Key not found\n");
} else {
printf("Key found, value = %d\n", tmp->data);
}
}
void display() {
int i;
for (i = 0; i < SIZE; i++) {
struct Node* tmp = hashTable[i];
printf("Slot[%d]: ", i);
while (tmp != NULL) {
printf("%d:%d -> ", tmp->key, tmp->data);
tmp = tmp->next;
}
printf("NULL\n");
}
}
int main() {
int choice, key, data;
while(1) {
printf("\n");
printf("1.Insert element into the table\n");
printf("2.Delete element from the table\n");
printf("3.Search element from the table\n");
printf("4.Display all elements of the table\n");
printf("5.Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter key: ");
scanf("%d", &key);
printf("Enter data: ");
scanf("%d", &data);
insert(key, data);
break;
case 2:
printf("Enter the key to delete: ");
scanf("%d", &key);
delete(key);
break;
case 3:
printf("Enter the key to search: ");
scanf("%d", &key);
search(key);
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
```
这个示例程序实现了哈希表的基础功能,包括插入元素、删除元素、搜索元素和显示哈希表内容。使用时,只需要创建一个哈希表数组,然后通过对键值进行哈希计算,在哈希表数组中寻找对应的位置,并在该位置上插入、搜索和删除数据即可。
相关推荐
![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)
![](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)