Creation of a hash table

Source: Internet
Author: User

Function: Create a hash table, and if there is a handle conflict, the element is placed in the re-hash method

Code Reference "0 basic data Structure"

The code is as follows:

[email protected]:/mnt/shared/appbox/hash# cat hash.c#include <stdlib.h> #include <stdio.h>#    Include <unistd.h> #include <malloc.h>typedef int keytype;typedef struct {KeyType key; /* Key value */int hi;        /* Hash counts */}datatype;typedef struct {DataType *data;  int tablesize;    /* Hash Table len */int cursize; /* Key value numbers */}hashtable;void Displayhash (HashTable *h, int m);/** h:hash table pointer* m:hashtable len* P:dev ided numbers* hash:be hashed data (src data) * N:number of key values*/void CreateHash (HashTable *h, int m, int p, int ha        sh[], int n) {int i, sum, addr, Di, k = 1;/* k:?/h->data = (DataType *) malloc (M * sizeof (DataType));                if (H->data = = NULL) {printf ("H->data is null!\n");        return;                } for (i=0; i<m; i++) {h->data[i].key =-1;        H->data[i].hi = 0; } for(i=0; i<n; i++)                {sum = 0;                addr = hash[i]% p;                Di = addr;                        if (H->data[addr].key = =-1) {h->data[addr].key = Hash[i];                        H->data[addr].hi = 1;                printf ("[line:%d] addr:%d, i=%d, key=%d\n", __line__, addr, I, hash[i]); } else {do {D                                i = (di + k)%m;                        sum + = 1;                        }while ((H->data[di].key! =-1));                        H->data[di].key = Hash[i];                        H->data[di].hi = sum + 1;                printf ("[line:%d] di:%d, i=%d, key=%d\n", __line__, Di, I, hash[i]);        }} h->cursize = n;        H->tablesize = m; Displayhash (H, m);}        void Displayhash (HashTable *h, int m) {int i; printf ("hasH index: ");        for (i=0; i<m; i++) printf ("%-5d", I);        printf ("\ n");        printf ("Key value:");        for (i=0; i<m;i++) printf ("%-5d", H->data[i].key);        printf ("\ n");        printf ("Hash times:");        for (i=0; i<m; i++) printf ("%-5d", H->data[i].hi); printf ("\ n");}        int main (int argc, char *argv[]) {int hash[] = {23, 35, 12, 56, 123, 39, 342, 90};        int m=11, p=11, n=8, POS;        HashTable H;        CreateHash (&h, M, p, hash, n); return 0;}  [email protected]:/mnt/shared/appbox/hash#

Output Result:

[Email protected]:/mnt/shared/appbox/hash#./hash    [line:59] addr:1, i=0, key=23[line:59] addr:2, I=1, Key=35[line : [] Di:3, i=2, key=12[line:70] di:4, i=3, key=56[line:70] di:5, i=4, key=123[line:59] addr:6, i=5, key=39[line:70] Di:7, I=6, key=342[line:70] di:8, i=7, key=90hash index:     0    1    2    3    4    5    6    7    8    9    Ten   Key value:      -1   123   342   -1   -1   hash times:     0    1    1    3    4    4    1    7    7    0    0   


Creation of a 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.