資料結構與演算法分析-開放定址散列表的實現

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   color   

#include<stdio.h>#include"fatal.h"typedef char* ElementType;typedef unsigned int Index;typedef Index Position;struct HashTbl;typedef struct HashTbl *HashTable;HashTable InitializeTable(int TableSize);void DestroyTable(HashTable H);Position Find(ElementType key,HashTable H);void Insert(ElementType key,HashTable H);ElementType Retrieve(Position P,HashTable H);HashTable Rehash(HashTable H);enum KindOfEntry {Legitimate,Empty,Deleted};struct HashEntry{    ElementType Element;    enum KindOfEntry Info;};typedef struct HashEntry Cell;struct HashTbl{    int TableSize;    Cell *TheCells;};int MinTableSize=23;HashTable InitializeTable(int TableSize){    HashTable H;    int i;    if(TableSize<MinTableSize)    {        Error("Table size too small!");        return NULL;    }    H=malloc(sizeof(struct HashTbl));    if(H==NULL)        FatalError("Out of space !!!");    H->TableSize=TableSize;    H->TheCells=malloc(sizeof(Cell)*H->TableSize);    if(H->TheCells==NULL)        FatalError("Out of space !!");    for(i=0;i<H->TableSize;i++)        H->TheCells[i].Info=Empty;    return H;}int Hash(ElementType key,int TableSize){    unsigned int HashVal=0;    while(*key!=‘\0‘)    {        HashVal=(HashVal<<5)+*key++;    }    HashVal=HashVal%TableSize;    return HashVal;}Position Find(ElementType key,HashTable H){    Position CurrentPos;    int CollisionNum;    CollisionNum=0;    CurrentPos=Hash(key,H->TableSize);    while(H->TheCells[CurrentPos].Info!=Empty&&H->TheCells[CurrentPos].Element!=key)    {        CurrentPos+=2*++CollisionNum-1;        if(CurrentPos>=H->TableSize)            CurrentPos-=H->TableSize;    }    return CurrentPos;}void Insert(ElementType key,HashTable H){    Position Pos;    Pos=Find(key,H);    if(H->TheCells[Pos].Info!=Legitimate)    {        H->TheCells[Pos].Info=Legitimate;        H->TheCells[Pos].Element=key;    }}ElementType Retrieve(Position P,HashTable H){    if(H->TheCells[P].Info!=Empty)        return H->TheCells[P].Element;    else        return NULL;}HashTable rehash(HashTable H){    int i,OldSize;    Cell* OldCells;    OldCells=H->TheCells;    OldSize=H->TableSize;    H=InitializeTable(2*OldSize);    for(i=0;i<OldSize;i++)        if(OldCells[i].Info==Legitimate)            Insert(OldCells[i].Element,H);    free(OldCells);    return H;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.