演算法學習 - Hash Table操作,分離連結法解決雜湊衝突

來源:互聯網
上載者:User

標籤:hashtable   分離連結法   演算法   

分離連結法

hash table是映射機制的,最大的優點就是它的操作是O(1)層級的。但是會出現雜湊衝突,這就需要幾種辦法來解決。這裡先說一種:分離連結法。

就是當插入的位置已經存在一個值之後,那麼在這個值之後插入,就可以了,也叫拉鏈法。(但是其實會降低尋找速度,變成O(n)層級)

下面是代碼:

////  main.cpp//  HashTable_SeparateChaining////  Created by Alps on 14-8-5.//  Copyright (c) 2014年 chen. All rights reserved.//#include <iostream>#include <math.h>#include "HashTable.h"#define MinTableSize 1using namespace std;bool Prime(int size){    for (int i = 2; i < sqrt(size); i++) {        if (size%i == 0) {            return false;        }    }    return true;}int NextPrime(int Tablesize){    if (Tablesize <= 2) {        return 2;    }else{        while (!Prime(Tablesize)) {            Tablesize++;        }        return Tablesize;    }    return Tablesize;}HashTable InitializeTable(int Tablesize){    HashTable H;    int i;        if (Tablesize < MinTableSize) {//        Error("table size is too small!");        exit(0);    }    H = (HashTable)malloc(sizeof(struct HashTb));    if (H == NULL) {        //Error("out of space");        exit(0);    }    H->Tablesize = NextPrime(Tablesize);    H->TheList = (List*)malloc(sizeof(List) * H->Tablesize);    for (i = 0; i < H->Tablesize; i++) {        H->TheList[i] = (List)malloc(sizeof(struct ListNode));        H->TheList[i]->Next = NULL;    }        return H;}int Hash(int key, int Tablesize){    return key%Tablesize;}Position Find(ElementType key, HashTable H){    int i = Hash(key, H->Tablesize);    List L = H->TheList[i]->Next;    while (L != NULL && L->element != key) {        L = L->Next;    }    return L;}void Insert(ElementType key, HashTable H){    if (Find(key, H) == NULL) {        int i = Hash(key, H->Tablesize);        List L = (List)malloc(sizeof(struct ListNode));        L->element = key;        Position P = H->TheList[i];        L->Next = P->Next;        P->Next = L;    }}int main(int argc, const char * argv[]){    HashTable H = InitializeTable(10);    printf("%d\n",H->Tablesize);    Insert(5, H);    Insert(3, H);    Position P = Find(5, H);    printf("%d\n",P->element);    return 0;}


下面是標頭檔:
////  HashTable.h//  HashTable_SeparateChaining////  Created by Alps on 14-8-5.//  Copyright (c) 2014年 chen. All rights reserved.//#ifndef HashTable_SeparateChaining_HashTable_h#define HashTable_SeparateChaining_HashTable_h#define ElementType intstruct ListNode;typedef struct ListNode *Position;struct HashTb;typedef HashTb *HashTable;HashTable InitializeTable(int Tablesize);void Destory(HashTable H);Position Find(ElementType key, HashTable H);void Insert(ElementType key, HashTable H);#endifstruct ListNode{    ElementType element;    Position Next;};typedef Position List;struct HashTb{    int Tablesize;    List * TheList;};



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.