Data structure---C language implementation hash list (hash table)

Source: Internet
Author: User
Hash table lookup Algorithm (hash) #include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 #define TRUE 1 #def    
INE FALSE 0 #define SUCCESS 1 #define UNSUCCESS 0 #define HASHSIZE 7 #define NULLKEY-32768 typedef int STATUS; 						typedef struct {int *elem; 						Base address int count;

The number of current data elements}hashtable; int m=0;
    Hash table LONG/* Initialize */Status init (HashTable *hashtable) {int i;
    M=hashsize; Hashtable->elem = (int *) malloc (M * sizeof (int));
    Request Memory hashtable->count=m;
    for (i=0;i<m;i++) {hashtable->elem[i]=nullkey;
return OK;

}/* Hash function (excluding remainder method) */int hash (int data) {return data% m;} /* Insert */void Insert (HashTable *hashtable,int data) {int hashaddress=hash (data);//hash address//conflict while (HASHTABL
    E->elem[hashaddress]!=nullkey) {//Resolve conflict hashaddress= (++hashaddress)%m using open addressable linear detection;
}//Insert value hashtable->elem[hashaddress]=data; }/* Find */int search (HashTable *hashtable,iNT data) {int hashaddress=hash (data);//hash address//conflict while (Hashtable->elem[hashaddress]!=data) {

        Solving conflict hashaddress= (++hashaddress)%m by using the open addressable linear detection method; if (hashtable->elem[hashaddress]==nullkey| |
    Hashaddress==hash (data)) return-1;
//Find successful return hashaddress;
    /* Print result/void Display (HashTable *hashtable) {int i;

    printf ("\n//==============================//\n");
    for (i=0;i 




Related Article

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.