The lookup code implementation of the hash table

Source: Internet
Author: User

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

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.