腾讯三面的时候,叫我写了个哈希表,当时紧张没写好···结果跪了···
回来后粪发涂墙,赶紧写了一个!
什么都不说了···先让我到厕所里面哭一会···
%>_<%
果然现场发挥,以及基础扎实才是important的!
用链地址法解决冲突的哈希表(C语言,VS2008编写、测试):
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <math.h> 4 #include <string.h> 5 6 struct node { 7 int count; // count the same value 8 char *value; 9 node *next; 10 }; 11 12 // 使用链地址法解决冲突 13 struct hash_table { 14 int size; // table size 15 node **list; // 链表队列一条链为一个散列位置 16 }; 17 18 //==================================================// 19 // declare 20 21 int hash_func(char *str, int tableSize); 22 hash_table *hash_table_init(int tableSize); 23 node *hash_table_new_node(char *str, int len); 24 int hash_table_insert(char *str, struct hash_table * head); 25 node *hash_table_find(char *str, struct hash_table * head); 26 void hash_table_clear(struct hash_table * head); 27 void hash_table_print(hash_table *head); 28 29 //==================================================// 30 // realize 31 32 // hash function: return the position of str in the hash table 33 int hash_func(char *str, int tableSize) { 34 unsigned int hashVal = 0; 35 36 while (*str != '