当前位置: 代码迷 >> 综合 >> C hashtable demo
  详细解决方案

C hashtable demo

热度:36   发布时间:2023-11-13 18:08:53.0

hashtable

  1. 链表数组:二级指针
  2. 基本操作 :初始化 插入 打印
#include <stdio.h>
#include <stdlib.h>
#include <string.h>typedef struct node {
    int data;struct node *next;
}Node, *ptrNode;int N = 10;
typedef Node **table;int main() 
{
    table hashTable = (table)malloc(N*sizeof(ptrNode));memset(hashTable, 0, sizeof(ptrNode)*N);int i, key, value;// insert valuewhile (1) {
    scanf("%d", &value);if (value == -1) break;ptrNode p = (ptrNode)malloc(sizeof(Node));p->data = value;key = value % 10;p->next = hashTable[key];hashTable[key] = p;}// dump tablefor (i = 0; i < N; i++) {
    ptrNode p = hashTable[i];printf("%d link: ", i);while (p) {
    printf("%d ", p->data);p = p->next;}printf("\n");}return 0;
}
  相关解决方案