1 //define the structure of a hash table2 #defineHashsize 12//Initial Size3 #defineNULLKEY-32768//default values, values that are not possible in the hash table4typedefstruct5 {6 int*elem;//base Address of the data element, evenly allocated array7 intEcount;//number of current data elements8 }hashtable;9 //Initialize Hash tableTen intInitHashTable (HashTable *H) One { AH->ecount=hashsize; -H->elem= (int*) malloc (hashsize*sizeof(int)); - if(! H->elem)//Build space Failed the { - return-1; - } - for(i=0; i) + { -h->elem[i]=Nullkey; + } A return 0; at } - //using the residue remainder method - intHash (intkey) - { - returnKey%hashsize; - } in //Insert a keyword into the list of columns - voidInserthash (HashTable *h,intKey//incoming hash list, the keyword to be inserted to { + intAddr//Address -Addr=hash (key);//remainder as address (offset address) the while(H->elem[addr]!=nullkey) {//if it is not empty, the conflict occurs and the default value is stored *Addr= (addr+1)%hashsize;//linear detection of open addressing method $ }Panax Notoginsengh->elem[addr]=key; - } the //list Find keywords + intSearchhash (HashTable H,intKeyint*ADDR)//the key,addr you want to find represents the address of the key you are looking for A { theXaddr=hash (key);//lookup efficiency is directly 1 + while(h.elem[*addr]!=key) - { $*addr= (xaddr+1)%hashsize; $ if(h.elem[*addr]==nullkey| | Xaddr==hash (Key))//H.elem[*addr]==nullkey representative finds the last element, Xaddr==hash (key)//represents the first element found, that is, the lookup element does not exist - { - return-1; the } - }Wuyi return 0; the}
The lookup code implementation of the hash table