Hash Table Initialization in C: A Complete Guide

In the C language, you can use an array of structures to initialize a hash table. Below is a simple sample code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 10

typedef struct {
    char key[20];
    int value;
} Entry;

Entry hashtable[SIZE];

void initHashtable() {
    for (int i = 0; i < SIZE; i++) {
        strcpy(hashtable[i].key, "");
        hashtable[i].value = -1;
    }
}

int main() {
    initHashtable();
    
    // 可以在这里进行散列表的操作
    // 例如插入、查找、删除等操作
    
    return 0;
}

In the example above, we defined a struct Entry containing key-value pairs and declared a struct array hashtable with a size of 10. The initHashtable function is used to initialize the hashtable, setting the keys and values of each element to their initial values.

In the main function, we can call the initHashtable function to initialize the hash table, and then perform operations such as insertion, searching, and deletion in this function.

bannerAds