資料結構與演算法分析-分離連結散列表的實現

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   ext   

#include<stdio.h>#include<math.h>typedef char* ElementType;typedef unsigned int Index;#define MinTableSize 15struct ListNode;typedef struct ListNode *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);void Delete(ElementType key,HashTable H);struct ListNode{    ElementType Element;    Position next;};typedef Position List;struct HashTbl{    int TableSize;    List *TheLists;};Index Hash(const char *key,int TableSize){    unsigned int HashVal=0;    while(*key!=‘\0‘)    {        HashVal+=*key++;    }    return HashVal%TableSize;}int NextPrime(int TableSize){    int i,j=2;    for(i=TableSize;i>0;i--)    {        while(j<sqrt(i))        {            if(i%j==0)                break;            j++;        }        if(j==sqrt(i))            break;    }    return i;}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=NextPrime(TableSize);    H->TheLists=malloc(sizeof(List)*H->TableSize);    if(H->TheLists==NULL)    {        FatalError("Out of space!!");    }    for(i=0;i<H->TableSize;i++)    {        //分配前端節點        H->TheLists[i]=malloc(sizeof(struct ListNode));        if(H->TheLists[i]==NULL)            FatalError("Out of space!!");        else            H->TheLists[i]->next=NULL;    }    return H;}void DestroyTable(HashTable H){    int i;    for(i=0;i<H->TableSize;i++)    {        free(H->TheLists[i]);    }    free(H->TheLists);    free(H);}Position Find(ElementType key,HashTable H){    Position P;    List L;    L=H->TheLists[Hash(key,H->TableSize)];    P=L->next;    while(P!=NULL&&P->Element!=key)        P=P->next;    return P;}void Insert(ElementType key,HashTable H){    Position Pos,NewCell;    List L;    Pos=Find(key,H);    if(Pos==NULL)    {        NewCell=malloc(sizeof(struct ListNode));        if(NewCell==NULL)        {            FatalError("Out of space!!");        }        else        {            L=H->TheLists[Hash(key,H->TableSize)];            NewCell->next=L->next;            NewCell->Element=key;            L->next=NewCell;        }    }}ElementType Retrieve(Position P){    if(P==NULL)        return NULL;    else        return P->Element;}void Delete(ElementType key,HashTable H){    Position Pos,Pre;    List L;    int i;    Pos=Find(key,H);    if(Pos==NULL)        return;    else    {        L=H->TheLists[Hash(key,H->TableSize)];        for(Pre=L;pre->next!=NUll;Pre=Pre->next)        {            if(Pre->next==Pos)            {                free(Pos->Element);                free(Pos);                break;            }        }    }}

 

聯繫我們

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