(5) hashtable <key, data> : hcreate hdestroy hsearch
来源:互联网 发布:阿里巴巴直接传淘宝 编辑:程序博客网 时间:2024/06/13 09:06
The three functions hcreate(), hsearch(), and hdestroy() allow the user to create a hash table (only one at a time) which associates a key with any data.
#include <stdio.h>#include <stdlib.h>#include <search.h>/* * * int hcreate(size_t nel); * void hdestroy(void); * * typedefstruct entry {char*key;void*data; } ENTRY; * ENTRY *hsearch(ENTRY item, ACTION action); * if not found * if (ACTION is specified with ENTER) * insert a copy of item * else if(ACTION is specified with FIND) * return NULL. */struct food{char *name;int price;};struct city_food{char *key;struct food most_famous_food;};typedef struct city_food city_food_t;city_food_t city_food_array[] = {{"beijing", {"douzhi", 2}},{"xian", {"yangroupaomo", 20}},{"tianjin", {"damahua", 5}},{"nanjing", {"yaxuefengsi", 8}},{"wuhan", {"reganmian", 3}},{"lanzhou", {"lamian", 7}},{"zhengzhou", {"huimian", 18}}};#define SIZE sizeof(city_food_array)/sizeof(city_food_t)int main(int argc, char **argv){ /* starting with small table, and letting it grow does not work */ hcreate(5); //create a hash table with an estimate of the number int i; ENTRY e, *ep; for (i = 0; i < SIZE; i++) { e.key = city_food_array[i].key; e.data = &city_food_array[i].most_famous_food; ep = hsearch(e, ENTER); //************ if(!ep){ fprintf(stderr, "failure started with #%d\n", i);// exit(1); //should not use exit, otherwise the latter coding will not run } } for(i = 0; i < SIZE; i++){ e.key = city_food_array[i].key; ep = hsearch(e, FIND);//************ if(ep){ printf("#%d, %s is found.\n", i, ep->key); printf("==={%s, {%s, %d}}\n", ep->key, ((struct food *)ep->data)->name, ((struct food *)ep->data)->price); }else{ printf("#%d, %s is not found.\n", i, e.key); } } hdestroy(); return 0;}
- (5) hashtable <key, data> : hcreate hdestroy hsearch
- 遍历 hashtable的key
- Java HashTable的key
- GNU hsearch (I)
- 存储 key/value的hashtable
- [Javascript Data Structures] HashTable 哈希表
- Data Structure-7 HashTable 哈希表
- 字符串做key的hashtable性能
- jQuery.data(element,[key],[value])
- 【Data Structures】 11. HashTable—Simple Implementation
- 通过引索获取hashtable中的key,value
- 哈希表(hashtable)通过值(value)获取查找键(key)
- java中hashTable和hashMap的Key的详解
- HashMap和HashTable,HashMap中key和value的原理
- 遍历Hashtable中的key和value的几种方法
- java.util.Hashtable.containsKey(Object key)方法实例
- HashMap和HashTable,HashMap中key和value的原理
- HashMap和HashTable,HashMap中key和value的原理
- (1) array of string/struct: qsort bsearch
- 【ACM】杭电1036:Average is not Fast Enough!
- 八皇后问题
- 发送POST请求代码---java转oc/c++
- spring3mvc与struts2的区别
- (5) hashtable <key, data> : hcreate hdestroy hsearch
- 内存管理——堆栈等内存的分布
- java的IO流转载
- 关于hdu1872的稳定排序
- JFreeChart的使用
- HDU 4431 Mahjong(枚举,模拟)
- Android的Notification应用详解和PendingIntent的初步剖解
- (2) array of sturct/string: lfind lsearch and insert
- (4) tree : tsearch tfind tdelete