Hash table template

Source: Internet
Author: User

# Include <conio. h> <br/> # include <memory. h> <br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> # define szname 80 <br/> # define hash_root 47/* random number used to calculate the hash address */<br/> # define szhash 50/* hash table total length */<br/> # define population 30/* Total number of students */</P> <p>/* hash table structure body */<br/> struct thash {< br/> int key; /* key code */<br/> char name [10];/* name */<br/> int depth;/* search depth */<br/> }; </P> <p>/* calculate the hash address based on the key code and hash root */<br /> Int gethashaddress (INT key, int root) <br/>{< br/> return key % root; <br/>}/ * end gethashaddress */</P> <p>/* Calculation of conflicting addresses. If an address conflict is found, use the current address, key code, and hash root to generate a new address */<br/> int getconflictaddress (INT key, int address, int root) <br/>{< br/> int ADDR = address + key % 5 + 1; <br/> return ADDR % root; <br/>}/ * end getconflictaddress */</P> <p>/* generate a hash key code based on the string, the method here is to accumulate all characters in the string in the numerical form and */<br/> int createkey (char * Name) <br/> {< Br/> int key = 0; <br/> unsigned char * n = (unsigned char *) Name; <br/> while (* n) key + = * n ++; <br/> return key; <br/>}/ * end createkey */</P> <p>/* enter a name, return the hash key code */<br/> int getname (char * Name) <br/>{< br/> scanf ("% s", name ); <br/> return createkey (name ); <br/>}/* end createkey */</P> <p>/* Create a hash table based on the number of students, length, and hash root */<br/> struct thash * createnames (INT size, int root, int population) <br/>{< br/> int I = 0, key = 0, ADDR = 0, depth = 0; char name [10]; <br/> struct thash * h = 0, * hash = 0; </P> <p>/* the hash root and length cannot be too small */<br/> If (size <root | root <2) return 0; </P> <p>/* construct an empty hash table based on the hash table length */<br/> hash = (struct thash *) malloc (sizeof (struct thash) * size); </P> <p>/* clear the entire table */<br/> memset (hash, 0, sizeof (struct thash) * size ); </P> <p> for (I = 0; I <population; I ++) {<br/>/* generates a random Student name, calculate the hash key code based on the name, and then calculate the address based on the key code */<br/> key = G Etname (name); <br/> ADDR = gethashaddress (Key, root); <br/> H = hash + ADDR; <br/> If (H-> depth = 0) {/* if the current hash address is not occupied, store the data */<br/> H-> key = key; <br/> strcpy (H-> name, name ); <br/> H-> depth ++; <br/> continue; <br/>}/* end if */<br/>/* If the hash address is already in use, that is, a conflict exists, find a new address, until not occupied */<br/> depth = 0; <br/> while (H-> depth) {<br/> ADDR = getconflictaddress (Key, ADDR, root); <br/> H = hash + ADDR; <br/> depth ++; <br/> }/* End while */<br/>/* store data according to the new address and record the retrieval depth */<br/> H-> key = key; <br/> strcpy (H-> name, name); <br/> H-> depth = depth + 1; <br/>}/ * Next */<br/> return hash; <br/>}/* end createnames */</P> <p>/* search for a student's record with a specific hash root in the hash table */<br/> struct thash * Lookup (struct thash * hash, char * Name, int root) <br/>{< br/> int key = 0, ADDR = 0; struct thash * h = 0; <br/>/* empty tables and names are not accepted */<br/> If (! Name |! Hash) return 0; <br/> key = createkey (name); <br/> ADDR = gethashaddress (Key, root); <br/> H = hash + ADDR; <br/>/* if the result is incorrect, the search will continue according to the conflict rule */<br/> while (strcmp (H-> name, name )) {<br/> ADDR = getconflictaddress (Key, ADDR, root); <br/> H = hash + ADDR; <br/> If (H-> key = 0) return 0; <br/>}/ * end while */<br/> return hash + ADDR; <br/>}/* end lookup */</P> <p>/* print the student information of the Record Based on a hash table record */<br/> void print (struct thash * R Ecord) <br/>{< br/> If (! Record) {<br/> printf ("[]/n"); <br/> return; <br/>}/ * end if */<br/> If (record-> depth) <br/> printf ("[key code] % 04d/T [name] % S/T [retrieval depth] % d/N", record-> key, record-> name, record-> depth); <br/> else <br/> printf ("[blank record]/n "); <br/>/* end if */<br/>}/* end print */</P> <p>/* print the student roster */<br/> void display (struct thash * hash, int size) <br/>{< br/> struct thash * h = 0; <br/> If (! Hash | size <1) return; <br/> printf ("student roster:/N"); <br/> printf ("--------------------/N "); <br/> for (H = hash; H <pash + size; H ++) {<br/> printf ("[address] % d/T ", h-hash); <br/> Print (h); <br/>}/ * Next */<br/> printf ("------------------/N "); <br/>}/ * end display */</P> <p>/* main function, program entry */<br/> int main (void) <br/> {<br/>/* hash table variable Declaration */<br/> struct thash * hash = 0, * h = 0; <br/> int cmd = 0;/* command */<br/> char name [10]; /* Student name */</P> <p>/* generate a hash table for 30 students */<br/> hash = createnames (szhash, hash_root, population ); <br/> for (;) {<br/> printf ("hash table display: 1-display hash table; 2-search name; other keys Exit: /n "); <br/> cmd = getch (); <br/> cmd-= '0'; <br/> switch (CMD) {<br/> case 1: Display (hash, szhash); break; <br/> case 2: <br/> printf ("enter the name of the student to search: "); <br/> scanf (" % s ", name); <br/> H = Lookup (hash, name, hash_root ); <br/> printf ("[address] % d/T", H-hash); <br/> Print (h); <br/> break; <br/> default: <br/> free (hash); <br/> return 0; <br/>}/ * end case */<br/>}/* Next */<br/> return 0; <br/>}/ * end main */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.