HashTable

HashTable

Separate Chaining

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef int ElementType;
typedef struct ListNode *Position;
typedef Position List;
struct HashTb1;
typedef struct HashTb1 *HashTable;

HashTable InitializeTable(int TableSize);
void DestroyTable(HashTable H);
int Delete(ElementType Key,HashTable H);
Position Find(ElementType Key,HashTable H);
void Insert(ElementType Key,HashTable H);
void Print_Hashtable(HashTable H);
//ElementType Retrieve(Position P);
ElementType NextPrime(ElementType TableSize);
int Hash(ElementType Key,int Size);
void Prime_init();

struct ListNode 
{
    ElementType Element;
    Position Next;
};

struct HashTb1
{
    int TableSize;
    List *TheLists;
};

const int N = 1e7 + 5;
bool isprime[N];
int prime[N];
int tot=1;
void Prime_init(){
    for(int i=2;i<N;i++){
        isprime[i] = true;
    }
    for(int i=2;i<N;i++){
        if(isprime[i]){
            for(int j=2*i;j<N;j=j+i){
                isprime[j]=false;
            }
        }
    }
    for(int i=2;i<N;i++){
        if(isprime[i]){
            prime[tot] = i;
            tot++;
        }
    }
}

ElementType NextPrime(ElementType TableSize)
{
    for(int i=0;i<tot;i++){
        if(TableSize<prime[i]) return prime[i];
    }
}

HashTable InitializeTable(int TableSize)
{
    HashTable H;
    int i;
//  if(TableSize<MinTableSize) return NULL;
    H=new struct HashTb1();
    if(H==NULL) return NULL;
    H->TableSize=NextPrime(TableSize);
    H->TheLists=new List[H->TableSize];
    for(int i=0;i<H->TableSize;i++){
        H->TheLists[i]=new struct ListNode();
    }
    return 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=new struct ListNode;
        L=H->TheLists[Hash(Key,H->TableSize)];
        NewCell->Next=L->Next;
        NewCell->Element=Key;
        L->Next=NewCell;
    }
}

int Hash(ElementType Key,int Size)
{
    return Key%Size;
}

void DestroyTable(HashTable H)
{
    Position P,tmp;
    for(int i=0;i<H->TableSize;i++){
        P=H->TheLists[i];
        while(P!=NULL){
            tmp=P->Next;
            delete P;
            P=tmp;
        }
    }
    delete[] H->TheLists;
    delete H;
}

int Delete(ElementType Key,HashTable H)
{
    Position P,tmp;
    List L;
    L=H->TheLists[Hash(Key,H->TableSize)];
    P=L;
    while(P!=NULL&&P->Next->Element!=Key) P=P->Next;
    if(P==NULL) return -1;
    tmp=P->Next;
    P->Next=P->Next->Next;
    delete tmp;
    return 1;
}

void Print_Hashtable(HashTable H)
{
    Position P;
    for(int i=0;i<H->TableSize;i++){
        P=H->TheLists[i];
        cout<<i<<": ";
        P=P->Next;
        while(P!=NULL){
            cout<<P->Element<<" ";
            P=P->Next;
        }
        cout<<endl;
    }
}

int main()
{
    Prime_init();
    int n;
    cout<<"請輸入要輸入的數據個數: ";
    cin>>n;
    HashTable H = InitializeTable(n);
    int x;
    for(int i=0;i<n;i++){
        cin>>x;
        Insert(x,H);
    }
    Print_Hashtable(H);
    DestroyTable(H);
    return 0;
}
相關文章
相關標籤/搜索