数据结构C++版 王红梅 OJ习题

1033: 散列表(1)

Description

已知Hash表的表长MaxSize为11,Hash函数为HashFunc(k)=k%11,冲突处理方法为线性探测法,部分代码如下,勿改动。请补充完成成员函数HashSearch,该函数的功能是动态查找k,若查找失败,则插入k,并返回查找失败所需的比较次数,若查找成功,返回查找k所需的比较次数,若表满,则抛出异常“Overflow”

#include <iostream>

using namespace std;

const int MaxSize=11; //hash表的表长,即教材中的m

class HashList

{

private:

int ht[MaxSize]; // hashtable

public:

int HashFunc(int k);  //hash function

HashList();     //consturctor

void Display();      //display

int HashSearch(int k); //dynamic search k

double HashASL();   //search ASL

};

//hash function

int HashList::HashFunc(int k)

{

return k%MaxSize;   //hash函数,假设为除余法,余数为MaxSize

}

//constructor:initialize an empty hashlist

HashList::HashList()

{

int i;

for(i=0;i<MaxSize;i++)

ht[i]=-1;  //-1 is empty

}

void HashList::Display()

{

int i;

for(i=0;i<MaxSize;i++)

cout<<ht[i]<<" ";

cout<<endl;

}

double  HashList::HashASL()

{

double ASL=0;

int i,n=0;

for(i=0;i<MaxSize;i++)

if(ht[i]!=-1)

{

ASL+=HashSearch(ht[i]);

cout<<ht[i]<<" "<<HashSearch(ht[i])<<endl;

n++;

}

// cout<<ASL<<" "<<n<<endl;

return ASL/n;

}

//在下面补充动态查找算法

int main()

{

    HashList HL;

// HL.Display();

while(1)

{

int k;

cin>>k;

if(!k) break;

try{

HL.HashSearch(k);

}

catch(const char *ms)

{

cout<<ms<<endl;

}

}

HL.Display();

cout<<"ASL="<<HL.HashASL();

return 0;

}

Input

输入数据以0结束,依次实现动态查找。

Output

输出数据 (各行数据依次为):

hash表中的数据的输出;

各个元素的值及其查找次数;

ASL值

Sample Input

47 7 29 11 16 92 22 8 3 29 0

Sample Output

11 22 -1 47 92 16 3 7 29 8 -1 
11 1
22 2
47 1
92 1
16 1
3 4
7 1
29 2
8 2
ASL=1.66667
//
// Created by Legends丶Hu on 2020/2/6.
//
#include <iostream>

using namespace std;

const int MaxSize = 11; //hash表的表长,即教材中的m
class HashList {
private:
    int ht[MaxSize]; // hashtable
public:
    int HashFunc(int k);  //hash function
    HashList();     //consturctor
    void Display();      //display
    int HashSearch(int k);  //dynamic search k
    double HashASL();   //search ASL
};

//hash function
int HashList::HashFunc(int k) {
    return k % 11;   //hash函数,假设为除余法,余数为11
}

//constructor:initialize an empty hashlist
HashList::HashList() {
    int i;
    for (i = 0; i < MaxSize; i++)
        ht[i] = -1;  //-1 is empty
}

void HashList::Display() {
    int i;
    for (i = 0; i < MaxSize; i++)
        cout << ht[i] << " ";
    cout << endl;
}

double HashList::HashASL() {
    double ASL = 0;
    int i, n = 0;
    for (i = 0; i < MaxSize; i++)
        if (ht[i] != -1) {
            ASL += HashSearch(ht[i]);
            cout << ht[i] << " " << HashSearch(ht[i]) << endl;
            n++;
        }
    return ASL / n;
}

int HashList::HashSearch(int k) {
    int j = HashFunc(k);
    if (ht[j] == k)
        return 1;
    else if (ht[j] == -1)
        ht[j] = k;
    else {
        int i = (j + 1) % MaxSize;
        int cnt = 1;
        while (ht[i] != -1 && i != j) {
            cnt++;
            if (ht[i] == k) {
                return cnt;
            } else
                i = (i + 1) % MaxSize;
        }
        if(i == j) throw "Overflow";
        else {
            ht[i] = k;
        }
    }
}
//47 7 29 11 16 92 22 8 3 29 0
int main() {
    HashList HL;
// HL.Display();
    while (1) {
        int k;
        cin >> k;
        if (!k) break;
        try {
            HL.HashSearch(k);
        }
        catch (const char *ms) {
            cout << ms << endl;
        }
    }
    HL.Display();
    cout << "ASL=" << HL.HashASL();
    return 0;
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值