int main() { int len = 15; //哈希表长,亦即表中槽的数目 printf("We set the length of hashtable %d\n",len); //创建哈希表并插入数据 HashTable hashtable = create_HashTable(len); if(insert_HashTable(hashtable,1)) printf("insert 1 success\n"); else printf("insert 1 fail,it is already existed in the hashtable\n"); if(insert_HashTable(hashtable,8)) printf("insert 8 success\n"); else printf("insert 8 fail,it is already existed in the hashtable\n"); if(insert_HashTable(hashtable,3)) printf("insert 3 success\n"); else printf("insert 3 fail,it is already existed in the hashtable\n"); if(insert_HashTable(hashtable,10)) printf("insert 10 success\n"); else printf("insert 10 fail,it is already existed in the hashtable\n"); if(insert_HashTable(hashtable,8)) printf("insert 8 success\n"); else printf("insert 8 fail,it is already existed in the hashtable\n"); //查找数据 pNode pFind1 = search_HashTable(hashtable,10); if(pFind1) printf("find %d in the hashtable\n",pFind1->data); else printf("not find 10 in the hashtable\n"); pNode pFind2 = search_HashTable(hashtable,4); if(pFind2) printf("find %d in the hashtable\n",pFind2->data); else printf("not find 4 in the hashtable\n"); //删除数据 if(delete_HashTable(hashtable,1)) printf("delete 1 success\n"); else printf("delete 1 fail"); pNode pFind3 = search_HashTable(hashtable,1); if(pFind3) printf("find %d in the hashtable\n",pFind3->data); else printf("not find 1 in the hashtable,it has been deleted\n"); //销毁哈希表 destroy_HashTable(hashtable,len); return 0; }
int main() { HashTable t; int a=1, b=2, c=3; HashTableElement* E1; HashTableElement* E2; HashTableElement* E3; //size 10 printf("bucket size is 1:\n"); init_HashTable(&t, 100); set_HashTable(&t, "one", &a); set_HashTable(&t, "two", &b); set_HashTable(&t, "there", &c); E1=get_HashTable(&t, "one"); E2=get_HashTable(&t, "two"); E3=get_HashTable(&t, "there"); if(E1!=NULL) { printf("%s:%d\n", E1->key, *(int*)(E1->valuePoint)); } if(E2!=NULL) { printf("%s:%d\n", E2->key, *(int*)(E2->valuePoint)); } if(E3!=NULL) { printf("%s:%d\n", E3->key, *(int*)(E3->valuePoint)); } delete_HashTable(&t, "two"); traverse_HashTable(&t, print_hash_element); set_HashTable(&t, "two", &b); traverse_HashTable(&t, print_hash_element); set_HashTable(&t, "two", &c); traverse_HashTable(&t, print_hash_element); clear_HashTable(&t); traverse_HashTable(&t, print_hash_element); destroy_HashTable(&t); //size 1 printf("bucket size is 1:\n"); init_HashTable(&t, 1); set_HashTable(&t, "one", &a); set_HashTable(&t, "two", &b); set_HashTable(&t, "there", &c); E1=get_HashTable(&t, "one"); E2=get_HashTable(&t, "two"); E3=get_HashTable(&t, "there"); if(E1!=NULL) { printf("%s:%d\n", E1->key, *(int*)(E1->valuePoint)); } if(E2!=NULL) { printf("%s:%d\n", E2->key, *(int*)(E2->valuePoint)); } if(E3!=NULL) { printf("%s:%d\n", E3->key, *(int*)(E3->valuePoint)); } delete_HashTable(&t, "two"); traverse_HashTable(&t, print_hash_element); set_HashTable(&t, "two", &b); traverse_HashTable(&t, print_hash_element); set_HashTable(&t, "two", &c); traverse_HashTable(&t, print_hash_element); clear_HashTable(&t); traverse_HashTable(&t, print_hash_element); destroy_HashTable(&t); //size 0 printf("bucket size is 0:\n"); if(!init_HashTable(&t, 0)) { printf("init error\n"); } return 0; }