c 哈希学习

本文介绍了一个使用C语言实现的简单词汇存储系统。该系统通过哈希表来存储不同类型的词汇,并提供了添加词汇及打印所有词汇的功能。文章展示了如何定义词汇结构体、实现哈希函数以及管理词汇链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  main.c

//  1

//

//  Created by cclyy on 14-7-19.

//  Copyright (c) 2014 cclyy. All rights reserved.

//


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>


struct _word

{

    int type;//1 ming 2 dong 3 xingrong

    char word[64];

    

    struct _word *ptNext;

};


typedef struct _word Word;


//new 1

Word *total_pw[100] = { NULL };


Word *pw = NULL;


void add (int t , char *w)

{

    Word *p1 = malloc(sizeof(Word));

    strcpy(p1->word, w);

    p1->type = t;

    p1->ptNext = NULL;

    

    //new 2

    int hash_index = t % 100;

    pw = total_pw[hash_index];

    

    if (pw == NULL) {

        pw = p1 ;

        

        //new 3

        total_pw[hash_index] = pw;

    }

    else

    {

        Word *ptmp = pw;

        

        while(1)

        {

            if (ptmp->ptNext == NULL) {

                break;

            }

            

            ptmp = ptmp->ptNext;

        }

        

        ptmp->ptNext = p1;

        

        

    }

}


void print()

{

    //new 4

    int i = 0;

    for(i = 0; i < 100; i ++)

    {

        //new 4 end

        

        Word *ppp = NULL;

        

        //new 5

        ppp=total_pw[i];

        //new 5 end

        

        while (1) {

            if (ppp == NULL) {

                break;

            }

            

            printf("%d%s\n",ppp->type,ppp->word);

            ppp = ppp->ptNext;

        }

    }

}


int main(int argc, const char * argv[]) {

    int n = 0;

    while (1) {

        if (n == 30) {

            break;

        }

        n++;

        add(n, "a");

    }

    print();

    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值