/* * hash.c * Creates a hash table using libc hash functions. User enters the name of a * state as a parameter and the corresponding state capital is returned from * a hash table lookup. * * NOTE: this example uses static global arrays as data for hash entries so we * dont need to worry about deallocating when destroying the hash table. * However, if you use dynamic memory for your hash entries you must maintain * an array of all entries created. The hdestroy function frees the memory used * by the hash table but does not deallocate the "data" pointer in the ENTRY * structure. e.g.: * * ENTRY my_entries[100]; * ... * for(i=0; i<100; i++) * free_data(my_entries.data); * hdestroy(); */ #include #include #include #include #include #include #define NUM_STATES 50 ENTRY statecapitals[NUM_STATES]; char *states[] = {"alabama","alaska","arizona","arkansas","california","colorado","connecticut", "delaware","florida","georgia","hawaii","idaho","illinois","indiana","iowa", "kansas","kentucky","louisiana","maine","maryland","massachusetts","michigan", "minnesota","mississippi","missouri","montana","nebraska","nevada","new hampshire", "new jersey","new mexico","new york","north carolina","north dakota","ohio","oklahoma", "oregon","pennsylvania","rhode island","south carolina","south dakota","tennessee","texas", "utah","vermont","virginia","washington","west virginia","wisconsin","wyoming"}; char *capitals[]={"Montgomery","Juneau","Phoenix","Little Rock","Sacramento","Denver","Hartford", "Dover","Tallahassee","Atlanta","Honolulu","Boise","Springfield","Indianapolis", "Des Moines","Topeka","Frankfort","Baton Rouge","Augusta","Annapolis","Boston", "Lansing","St. Paul","Jackson","Jefferson City","Helena","Lincoln","Carson City", "Concord","Trenton","Santa Fe","Albany","Raleigh","Bismarck","Columbus","Oklahoma City", "Salem","Harrisburg Island","Providence","Columbia","Pierre","Nashville","Austin", "Salt Lake City","Montpelier","Richmond","Olympia","Charleston","Madison","Cheyenne"}; int main(int argc, char **argv) { ENTRY search; ENTRY *ret; int i; char *statebuf; if(argc!=2) { fprintf(stdout,"usage: %s \n",argv[0]); fflush(stdout); return -1; } hcreate(NUM_STATES); for(i=0; idata); fflush(stdout); } free(statebuf); hdestroy(); return 0; }