• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

数据结构-使用哈希存储数据存入哈希表,并进行查找

武飞扬头像
Coding Peasant
帮助1

学新通

主函数

  1.  
    #include "./fun.h"
  2.  
     
  3.  
    int main(int argc, const char *argv[])
  4.  
    {
  5.  
    int arr[10] = {25,51,8,22,26,67,11,16,54,41};
  6.  
     
  7.  
    Node *hash[N]; //定义哈希表
  8.  
     
  9.  
    hash_init(hash);
  10.  
     
  11.  
    //循环调用函数插入
  12.  
    for(int i=0; i<10; i )
  13.  
    {
  14.  
    hash_insert(hash, arr[i]);
  15.  
    }
  16.  
     
  17.  
    //调用输出哈希表函数
  18.  
    hash_show(hash);
  19.  
     
  20.  
    //调用hash查找函数
  21.  
    while (1)
  22.  
    {
  23.  
    int num;
  24.  
    printf("请输入要查找的数值:");
  25.  
    scanf("%d",&num);
  26.  
    hash_search(hash, num);
  27.  
    }
  28.  
    return 0;
  29.  
    }

被调函数 - "fun.h"

  1.  
    #ifndef __FUN_H__
  2.  
    #define __FUN_H__
  3.  
     
  4.  
    #define N 13
  5.  
     
  6.  
    #include <stdio.h>
  7.  
    #include <stdlib.h>
  8.  
     
  9.  
    //定义节点结构体类型
  10.  
    typedef struct Node
  11.  
    {
  12.  
    int data; //数据域
  13.  
    struct Node *next; //指针域
  14.  
    }Node;
  15.  
     
  16.  
    //初始化哈希表
  17.  
    void hash_init(Node *hash[]);
  18.  
     
  19.  
    //将数据存入哈希表
  20.  
    void hash_insert(Node *hash[], int key);
  21.  
     
  22.  
    //输出哈希表内容
  23.  
    void hash_show(Node *hash[]);
  24.  
     
  25.  
    //哈希查找
  26.  
    void hash_search(Node *hash[], int key);
  27.  
     
  28.  
     
  29.  
    #endif

被调函数 - "fun.c"

  1.  
    #include "./fun.h"
  2.  
     
  3.  
    //初始化哈希表
  4.  
    void hash_init(Node *hash[])
  5.  
    {
  6.  
    for(int i=0; i<N; i )
  7.  
    {
  8.  
    hash[i] = NULL;
  9.  
    }
  10.  
    }
  11.  
     
  12.  
    //将数据存入哈希表
  13.  
    void hash_insert(Node *hash[], int key)
  14.  
    {
  15.  
    int pos = key%N; //找到要存储的链表位置
  16.  
     
  17.  
    //申请节点封装数据
  18.  
    Node *p = (Node *)malloc(sizeof(Node));
  19.  
    if(NULL==p)
  20.  
    {
  21.  
    printf("节点申请失败\n");
  22.  
    return ;
  23.  
    }
  24.  
    p->data = key;
  25.  
    p->next = NULL;
  26.  
     
  27.  
    //头插法将数据放入表中
  28.  
    p->next = hash[pos];
  29.  
    hash[pos] = p;
  30.  
    }
  31.  
     
  32.  
    //输出哈希表内容
  33.  
    void hash_show(Node *hash[])
  34.  
    {
  35.  
    //遍历每一条链表
  36.  
    for(int i=0; i<N; i )
  37.  
    {
  38.  
    printf("%d:", i);
  39.  
    //定义遍历指针,将当前链表遍历
  40.  
    Node *q = hash[i];
  41.  
    while(q!=NULL)
  42.  
    {
  43.  
    printf("%d ---> ", q->data);
  44.  
    q=q->next;
  45.  
    }
  46.  
    printf("NULL\n");
  47.  
    }
  48.  
    }
  49.  
     
  50.  
    //哈希查找
  51.  
    void hash_search(Node *hash[], int key)
  52.  
    {
  53.  
    //通过哈希函数,确定要查找值所在的链表
  54.  
    int pos = key%N;
  55.  
     
  56.  
    //定义遍历指针遍历当前链表
  57.  
    Node *q = hash[pos];
  58.  
    while(q!=NULL && q->data!=key)
  59.  
    {
  60.  
    q = q->next;
  61.  
    }
  62.  
     
  63.  
    //判断q是否为空
  64.  
    if(q == NULL)
  65.  
    {
  66.  
    printf("您要查找的数据不在表中\n");
  67.  
    }else
  68.  
    {
  69.  
    printf("您要查找的数据在表中\n");
  70.  
    }
  71.  
    }

测试 

  1.  
    root@VM-12-9-ubuntu:hx# a
  2.  
    0:26 ---> NULL
  3.  
    1:NULL
  4.  
    2:41 ---> 54 ---> 67 ---> NULL
  5.  
    3:16 ---> NULL
  6.  
    4:NULL
  7.  
    5:NULL
  8.  
    6:NULL
  9.  
    7:NULL
  10.  
    8:8 ---> NULL
  11.  
    9:22 ---> NULL
  12.  
    10:NULL
  13.  
    11:11 ---> NULL
  14.  
    12:51 ---> 25 ---> NULL
  15.  
    请输入要查找的数值:15
  16.  
    您要查找的数据不在表中
  17.  
    请输入要查找的数值:25
  18.  
    您要查找的数据在表中
  19.  
    请输入要查找的数值:51
  20.  
    您要查找的数据在表中
  21.  
    请输入要查找的数值:8
  22.  
    您要查找的数据在表中
  23.  
    请输入要查找的数值:99
  24.  
    您要查找的数据不在表中
  25.  
    请输入要查找的数值:0
  26.  
    您要查找的数据不在表中

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfgjfc
系列文章
更多 icon
同类精品
更多 icon
继续加载