c语言 哈希表创建,删除,添加,查询

本例中的哈希算法是一个演示用途,1,算法,2,取模 ,3 取余。此算法只是针对本题的例子,此算法并不能很好的分配数据。主要是练习一个哈希表的框架结构,要注意的是每个哈希表里的数据都要写在链表中,需要用malloc来定义,不然之后free会遇到无法释放的问题。

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

#define MAX_NAME 256
#define TABLE_SIZE 10

typedef struct person
{
    char name[MAX_NAME];
    int age;
    struct person * next;
    struct person * prev;
    struct person * last;
}person;

void init_hash_table(person * hh[])
{
   for (int i = 0 ; i < TABLE_SIZE ; i++)
    {
        hh[i] = NULL;
    }
}

void print_hash_table(person * hh[])
{
   for(int i = 0 ; i < TABLE_SIZE ; i++)
   {
       if(hh[i] != NULL)
        {
          if(hh[i]->last != NULL)
          {
              person *p = hh[i];
              printf("hh[%d]",i);
              while(p)
              {
                   printf("--%s--%d",p-&g
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的基于哈实现的电话号码查询系统的C语言源代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define TABLE_SIZE 100 typedef struct node { char name[20]; char phone[20]; struct node *next; } Node; Node *table[TABLE_SIZE]; unsigned int hash(char *str) { unsigned int hash = 5381; int c; while ((c = *str++)) hash = ((hash << 5) + hash) + c; return hash % TABLE_SIZE; } Node *find(char *name) { unsigned int index = hash(name); Node *p; for (p = table[index]; p != NULL; p = p->next) if (strcmp(name, p->name) == 0) return p; return NULL; } void insert(char *name, char *phone) { unsigned int index = hash(name); Node *p; for (p = table[index]; p != NULL; p = p->next) if (strcmp(name, p->name) == 0) { strcpy(p->phone, phone); return; } p = (Node *) malloc(sizeof(Node)); strcpy(p->name, name); strcpy(p->phone, phone); p->next = table[index]; table[index] = p; } void delete(char *name) { unsigned int index = hash(name); Node *p, *q; for (q = NULL, p = table[index]; p != NULL; q = p, p = p->next) if (strcmp(name, p->name) == 0) { if (q == NULL) table[index] = p->next; else q->next = p->next; free(p); return; } } void display() { int i; Node *p; for (i = 0; i < TABLE_SIZE; ++i) for (p = table[i]; p != NULL; p = p->next) printf("%s\t%s\n", p->name, p->phone); } int main() { char name[20], phone[20]; int choice; while (1) { printf("\nTelephone directory\n"); printf("1. Find\n2. Insert\n3. Delete\n4. Display\n5. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter name: "); scanf("%s", name); Node *p = find(name); if (p) printf("%s\t%s\n", p->name, p->phone); else printf("Name not found\n"); break; case 2: printf("Enter name: "); scanf("%s", name); printf("Enter phone number: "); scanf("%s", phone); insert(name, phone); break; case 3: printf("Enter name: "); scanf("%s", name); delete(name); break; case 4: display(); break; case 5: exit(0); default: printf("Invalid choice\n"); } } return 0; } ``` 该程序使用了一个大小为100的哈来存储电话号码。它实现了四个基本操作:查找、插入、删除和显示。在运行程序时,用户可以选择要执行的操作。如果选择插入操作,则需要输入姓名和电话号码。如果选择查找操作,则需要输入姓名,程序将输出该姓名对应的电话号码。如果选择删除操作,则需要输入姓名,程序将从哈删除该用户的信息。如果选择显示操作,则程序将输出哈中所有用户的姓名和电话号码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值