首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

用C和Java实现哈希表

哈希表是一种常见的数据结构,用于存储键值对。它通过将键映射到一个固定大小的数组索引来实现快速的插入、查找和删除操作。在C和Java中,我们可以使用不同的方法来实现哈希表。

在C中,我们可以使用数组和链表的组合来实现哈希表。具体步骤如下:

  1. 定义一个固定大小的数组,用于存储链表的头节点。
  2. 创建一个哈希函数,将键映射到数组索引。
  3. 当插入一个键值对时,通过哈希函数计算出数组索引,然后将键值对插入到对应链表的末尾。
  4. 当查找或删除一个键值对时,通过哈希函数计算出数组索引,然后在对应链表中遍历查找或删除。

以下是C语言实现哈希表的示例代码:

代码语言:txt
复制
#include <stdio.h>
#include <stdlib.h>

#define SIZE 10

// 定义哈希表节点
typedef struct Node {
    int key;
    int value;
    struct Node* next;
} Node;

// 创建哈希表
Node** createHashTable() {
    Node** hashTable = (Node**)malloc(sizeof(Node*) * SIZE);
    for (int i = 0; i < SIZE; i++) {
        hashTable[i] = NULL;
    }
    return hashTable;
}

// 哈希函数
int hashFunction(int key) {
    return key % SIZE;
}

// 插入键值对
void insert(Node** hashTable, int key, int value) {
    int index = hashFunction(key);
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->key = key;
    newNode->value = value;
    newNode->next = NULL;

    if (hashTable[index] == NULL) {
        hashTable[index] = newNode;
    } else {
        Node* curr = hashTable[index];
        while (curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = newNode;
    }
}

// 查找键对应的值
int find(Node** hashTable, int key) {
    int index = hashFunction(key);
    Node* curr = hashTable[index];
    while (curr != NULL) {
        if (curr->key == key) {
            return curr->value;
        }
        curr = curr->next;
    }
    return -1;  // 未找到
}

// 删除键值对
void remove(Node** hashTable, int key) {
    int index = hashFunction(key);
    Node* curr = hashTable[index];
    Node* prev = NULL;
    while (curr != NULL) {
        if (curr->key == key) {
            if (prev == NULL) {
                hashTable[index] = curr->next;
            } else {
                prev->next = curr->next;
            }
            free(curr);
            return;
        }
        prev = curr;
        curr = curr->next;
    }
}

// 打印哈希表
void printHashTable(Node** hashTable) {
    for (int i = 0; i < SIZE; i++) {
        printf("Index %d: ", i);
        Node* curr = hashTable[i];
        while (curr != NULL) {
            printf("(%d, %d) ", curr->key, curr->value);
            curr = curr->next;
        }
        printf("\n");
    }
}

int main() {
    Node** hashTable = createHashTable();

    insert(hashTable, 1, 10);
    insert(hashTable, 2, 20);
    insert(hashTable, 11, 30);
    insert(hashTable, 21, 40);

    printf("哈希表:\n");
    printHashTable(hashTable);

    int value = find(hashTable, 11);
    printf("键11对应的值为:%d\n", value);

    remove(hashTable, 2);

    printf("删除键2后的哈希表:\n");
    printHashTable(hashTable);

    return 0;
}

在Java中,我们可以使用Java集合框架中的HashMap类来实现哈希表。HashMap类已经封装了哈希函数和相关操作,使用起来更加方便。以下是Java语言实现哈希表的示例代码:

代码语言:txt
复制
import java.util.HashMap;

public class HashTableExample {
    public static void main(String[] args) {
        HashMap<Integer, Integer> hashTable = new HashMap<>();

        hashTable.put(1, 10);
        hashTable.put(2, 20);
        hashTable.put(11, 30);
        hashTable.put(21, 40);

        System.out.println("哈希表:");
        System.out.println(hashTable);

        int value = hashTable.get(11);
        System.out.println("键11对应的值为:" + value);

        hashTable.remove(2);

        System.out.println("删除键2后的哈希表:");
        System.out.println(hashTable);
    }
}

以上就是用C和Java实现哈希表的方法和示例代码。哈希表在实际开发中广泛应用于数据存储和查找的场景,例如缓存、索引、字典等。腾讯云提供了多种云计算相关产品,如云数据库、云服务器、云存储等,可以根据具体需求选择适合的产品。更多关于腾讯云产品的信息,请参考腾讯云官方网站:https://cloud.tencent.com/

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

9分59秒

087-尚硅谷-图解Java数据结构和算法-哈希表实现思路图解

22分17秒

088-尚硅谷-图解Java数据结构和算法-哈希表代码实现(1)

28分38秒

089-尚硅谷-图解Java数据结构和算法-哈希表代码实现(2)

9分59秒

087-尚硅谷-图解Java数据结构和算法-哈希表实现思路图解

22分17秒

088-尚硅谷-图解Java数据结构和算法-哈希表代码实现(1)

28分38秒

089-尚硅谷-图解Java数据结构和算法-哈希表代码实现(2)

12分14秒

50-尚硅谷-Scala数据结构和算法-哈希(散列)表实现机制分析

10分29秒

086-尚硅谷-图解Java数据结构和算法-哈希表的介绍和内存布局

10分29秒

086-尚硅谷-图解Java数据结构和算法-哈希表的介绍和内存布局

14分33秒

Java零基础-224-哈希表数据结构

40分48秒

Golang教程 数据结构和设计模式 28 哈希表 学习猿地

1分24秒

C语言实现九九乘法表

领券