linked hash list test with C

 1 #ifndef _HASH_H_
 2 #define _HASH_H_
 3 #include <stdbool.h>
 4 #include <stdlib.h>
 5 #define SIZE 26
 6 
 7 typedef struct node{
 8     char * word;
 9     size_t count;
10     struct node * next;
11 }Node, * pnode;
12 
13 typedef struct words{
14     Node * head[SIZE];
15 }Words;
16 
17 void init_words(Words * words);
18 
19 void destroy_words(Words * words);
20 
21 size_t hash_fun(const char * word);
22 
23 bool append_word(Words * words, const char * word);
24 
25 bool delete_word(Words * words, char * word);
26 
27 void print_words(Words * words);
28 
29 #endif
hash.h
  1 #include "hash.h"
  2 #include <string.h>
  3 #include <stdio.h>
  4 
  5 
  6 static Node * _make_node(const char * word, size_t count, Node * next);
  7 
  8 static void _add_node(Node * head, Node * newNode);
  9 
 10 static void _delete_next_node(Node * prev);
 11 
 12 static Node * _make_node(const char * word, size_t count, Node * next)
 13 {
 14 
 15     char * newStr = (char*)malloc(sizeof(char) * strlen(word));
 16     memcpy(newStr, word, strlen(word));
 17     Node * newNode = (Node *)malloc(sizeof(Node));
 18     newNode->count = count;
 19     newNode->word = newStr;
 20     newNode->next = next;
 21     return newNode;
 22 }
 23 
 24 static void _add_node(Node * head, Node * newNode)
 25 {
 26     newNode->next = head->next;
 27     head->next = newNode;
 28 }
 29 
 30 static void _delete_next_node(Node * prev)
 31 {
 32     Node * delNode = prev->next;
 33     prev->next = delNode->next;
 34     free(delNode->word);
 35     free(delNode);
 36 }
 37 
 38 void init_words(Words * words)
 39 {
 40     for(int i = 0; i < SIZE; i++)
 41         words->head[i] = _make_node("",0,NULL);
 42 }
 43 
 44 void destroy_words(Words * words)
 45 {
 46     for(int i = 0; i < SIZE; i++)
 47     {
 48         Node * p = words->head[i];
 49         while(p->next)
 50             _delete_next_node(p);
 51         free(p->word);
 52         free(p);
 53     }
 54 }
 55 
 56 size_t hash_fun(const char * word)
 57 {
 58     return (word[0] - 'a') % SIZE;    
 59 }
 60 
 61 bool append_word(Words * words,const char * word)
 62 {
 63     size_t index = hash_fun(word);
 64     Node * current = words->head[index]->next;
 65     while(current)
 66     {
 67         if(!strcmp(current->word, word))
 68         {
 69             current->count++;
 70             return false;
 71         }
 72         else current = current->next;
 73     }
 74     Node * newNode = _make_node(word,1,NULL);
 75     _add_node(words->head[index], newNode);
 76     words->head[index]->count++;
 77     return true;
 78 }
 79 
 80 bool delete_word(Words * words, char * word)
 81 {
 82     size_t index = hash_fun(word);
 83     Node * current = words->head[index];
 84     while(current->next)
 85     {
 86         if(!strcmp(current->next->word, word))
 87         {
 88             words->head[index]->count--;
 89             _delete_next_node(current);
 90             return true;
 91         }
 92         current = current->next;
 93     }
 94     return false;
 95 }
 96 
 97 void print_words(Words * words)
 98 {
 99     for(int i = 0; i < SIZE; i++)
100     {
101         if(!words->head[i]->count)
102             continue;
103         for(Node * p = words->head[i]; p; p=p->next)
104             printf("(%s, %zu) ", p->word, p->count);
105         printf("\n");    
106     }
107     printf("\n");
108 }
hash.c
 1 #include <stdio.h>
 2 #include "hash.h"
 3 
 4 
 5 int main()
 6 {
 7     char str[][10] = {"good","google","good","been", "between","food","call","been","calloc","destroy","xo","yellow","zero","zoom"};
 8     int strNum = sizeof(str)/sizeof(str[0]);
 9     Words w;
10     init_words(&w);
11     for(int i = 0; i < strNum; i++)
12     {
13         append_word(&w, str[i]);
14     }
15     print_words(&w);
16     delete_word(&w, "google");
17     delete_word(&w, "been");
18     delete_word(&w, "zero");
19     printf("**************************\n");
20     print_words(&w);
21     destroy_words(&w);
22     return 0;
23 }
main.c

 

posted @ 2018-03-05 22:32  黑马网仔  阅读(159)  评论(0编辑  收藏  举报