c 哈希学习

来源:互联网 发布:ubuntu cpuz 编辑:程序博客网 时间:2024/05/16 13:51

//

//  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;

}


0 0
原创粉丝点击