数据结构与算法——散列表类的C++实现(探测散列表)

散列表简介:

散列表的实现常被称为散列。散列是一种用于以常数平均时间执行插入、删除和查找的技术。

散列的基本思想:

理想的散列表数据结构只不过是一个包含一些项的具有固定大小的数组。(表的大小一般为素数)
设该数组的大小为TbaleSize,我们向该散列表中插入数据,首先我们将该数据用一个函数(散列函数)映射一个数值x(位于0到TbaleSize1-1之间);然后将该数据插入到散列表的第x的单元。(如果有多个数据映射到同一个数值,这个时候就会发生冲突)

散列函数介绍:

为了避免散列函数生成的值不是均匀分布,有一个比较好的散列函数可以使用。
在该散列函数中涉及数据中所有的字符,并且一般可以分布的很好,它计算的是0到KeySize-1进行求和Key[KeySize-i-1]*(37^i);

下面是该散列函数的实现:

/****************************************************************
*   函数名称:hash(const string & key, int tableSize)
*   功能描述: 根据键值求个hash值 
*   参数列表: key 数据项的键值 
*             tableSize 散列表的大小
*   返回结果:返回hash值
*****************************************************************/
int hash(const string & key, int tableSize)
{
    int hashVal = 0;

    //用散列函数的那个公式求和
    for(int i = 0; i < key.length(); ++i)
        hashVal = 37*hashVal + key[i];

    hashVal %= tableSize;//求得的hash值

    if(hashVal < 0)
        hashVal += tableSize;

    return hashVal;
}

散列函数完成之后下面就是解决hash值的冲突问题。


解决冲突的方法主要有:分离链接法和开放定址法


分离链接法:分离链接法


开放定址法:

发生冲突的时候分离链接法需要给新的单元分配新的地址空间,这会导致算法导致算法速度减慢。还有另外一种思路可以解决hash值的冲突问题,当发生冲突的时候就尝试选择另外一个单元,直到找到空的单元。也就是依次尝试h0(x), h1(x), h2(x).....,其中hi(x) = (hash(x) + f(i)) % TableSize,且f(0)=0,直到找到一个hi(x)单元是空的。函数f是冲突解决函数。
因为此办法是将所有的元素都放入散列表中,分离链接法是将有相同hash值的元素放在一个链表里面,所以该方法要比分离链接法需要的散列表大。 对于此方法的解决冲突方案需要的散列表的装填因子应该低于0.5。装填因子是散列表中的元素个数与散列表的大小的比值。所以该方法需要的 散列表的大小应该大于等于元素个数的两倍。
我们称使用此方法的散列表为探测散列表(probing hash tables).

探测散列表有三种常见的解决冲突的方法:线性探测、平方探测、双散列。

线性探测:

线性探测中函数f是i的线性函数,一般情况下为f(i) = i。相当于是逐个探测散列表的单元直到查找出空单元。
下图是一个例子:



在上面的例子中,第一个冲突发生在插入49的时候,h0(49)=9,但是此时第9个位置已经被占用了,所以只能继续探测,直到找到一个空的位置。下一次探测的位置是h1(49)=0,第0个位置是空的,所有将49插入到第0个位置。插入58的时候探测了三次才找到了一个空位置。其它冲突类似。
经过统计,探测散列表如有多于一半的空间被填满的时候,那么线性探测就不是一个好方法了,此时插入和查找需要的探测次数比较多。如果装填因子是0.5,那么插入操作平均只需要2.5次探测,并且对于成功的查找平均只需要1.5次探测。
线性探测可能会出现一次聚集的问题,因为散列表被占用的空间会形成一块连续的区域。这样的话需要探测的次数就会比较多,因为探测需要经过该连续区域。


平方探测:

平方探测是消除线性探测中一次聚集问题的冲突解决方法。平方探测就是冲突函数f为二次函数。一般f(i) = i*i;

下面是一个和上面一样的例子,见下图:


经过证明:如果表的一半是空的,并且表的大小为素数,那么可以保证总能够插入一个新的元素。
平方探测虽然排除了一次聚集,但是散列到同一位置上的那些元素将探测相同的备选单元,这称为二次聚集。该问题可以通过下面的双散列方法解决。


双散列:

双散列一般冲突解决函数f(i) = i * hash2(x);
这又涉及到第二个散列函数hash2(x)的选择,一般我们选择hash2(x) = R - (x % R),其中R是小于TableSize的素数。


下面是一个和上面一样的例子,见下图:
我们选择R=7,因为TableSize=10,一般TableSize为素数,这里为了简单,将TableSize设置为10。




该探测散列表的成员结构为:

 //散列表的数据单元结构
struct HashEntry{
    HashedObj element;//该散列表存放的数据
    EntryType info;//表明该位置的状态 

    HashEntry(const HashedObj & e = HashedObj(), EntryType i = EMPTY):element(e), info(i){}
};

其中info有三种取值,{ACTIVE, EMPTY, DELETED};//每个数据单元都有一个info变量,表明该位置是否被占用、空或已删除。


散列表类的主要成员函数:

bool containes(const HashedObj & x);//判断是否包含数据项x
void makeEmpty();//清空散列表
bool isEmpty();
bool insert(const HashedObj & x);//插入项x
bool remove(const HashedObj & x);//删除项x

void print();//输出散列表中的内容
int findPos(const HashedObj & x);//根据名字查找数据项
HashedObj findElement(const HashedObj & x);//根据名字查找数据项,并返回

void rehash();//再散列
int myhash(const HashedObj & x) const;//散列函数
int nextPrime(int n);//求的距离N最近的一个大于N的素数
int prePrime(int n);//求距离N最近的一个小于N的素数
bool isActive(int currentPos) const;//判断位置currentPos处的是否有元素

主要成员函数介绍:

1、再散列rehash():

当元素的个数大于散列表大小的一半的时候,需要扩大散列表的大小为原来的二倍。先保存原来的数据,再扩大原来的空间,再将原来的数据重新插入到新的散列表中。(此时一定要用insert成员函数,不能直接拷贝,因为涉及到hash值的重新计算)

/****************************************************************
*   函数名称:rehash()
*   功能描述: 扩大散列表的大小
*   参数列表: 无
*   返回结果:无 
*****************************************************************/
template<typename HashedObj>
void HashTable<HashedObj>::rehash()
{    
    vector<HashEntry> oldArray = array;

    //创建一个新的大小为原来两倍大小的散列表
    array.resize(nextPrime(2 * oldArray.size()));

    for(int i = 0; i < array.size(); ++i)
        array[i].info = EMPTY;

    currentSize = 0;
    //复制散列表
    for(int i = 0; i < oldArray.size(); ++i){
        if(oldArray[i].info == ACTIVE)
            insert(oldArray[i].element);
    }
}

2、查找元素的位置findPos():

该函数是查找一个元素应该处于散列表中的位置。当要插入元素的时候,首先调用该函数找到了空位置,然后insert函数在该空位置插入元素。如果要插入的元素存在,则也返回该位置,然后insert函数中会判断是否已经存在该元素,如果已经存在则返回false,否则插入该元素。
该函数有三种解决冲突的方法:线性探测、平方探测、双散列。也就是实现了三种f(i)函数。
//currentPos += offset;//线性探测
currentPos += 2 * offset -1;//平方探测
//currentPos += prePrime(array.size()) - myhash(x)%prePrime(array.size());//双散列

本例中使用了平方探测,如果想用其它探测方式,将该方式去除注释,就把另外两种方式屏蔽。

/****************************************************************
*   函数名称:findPos(const HashedObj & x) const
*   功能描述: 查找x应该插入的位置
*   参数列表: x是要插入的元素
*   返回结果:如果找到空的位置则返回要插入的位置标号
*****************************************************************/
template<typename HashedObj>
int HashTable<HashedObj>::findPos(const HashedObj & x)
{
    //线性探测f(i) = i; f(i) = f(i-1) + 1;相隔为1
    //平方探测f(i) = i*i; f(i) = f(i-1) + 2*i - 1; 相隔为2*i-1
    //双散列,f(i) = i*hash2(x); f(i) = f(i-1)+hash2(x);相隔为hash2(x);
    //hash2(x) = R-(x%R); R=prePrime(array.size()),R为小于TableSize()的素数
    int offset = 1;

    int currentPos = myhash(x);

    //如果找到了空的位置则返回位置标号
    //如果找到了该元素x,则返回该元素的位置标号
    while(array[currentPos].info != EMPTY && array[currentPos].element != x){
         //currentPos += offset;//线性探测
         currentPos += 2 * offset -1;//平方探测
         //currentPos += prePrime(array.size()) - myhash(x)%prePrime(array.size());//双散列
         offset++;

        if(currentPos >= array.size())
            currentPos -= array.size();
    }
    
    return currentPos;
}

3、插入函数insert():

/****************************************************************
*   函数名称:insert(const HashedObj & x)
*   功能描述: 在散列表中插入元素x,如果插入项已经存在,则什么都不做。
*             否则将其放在表的前端
*   参数列表: x数据项
*   返回结果:插入成功返回true, 否则返回false
*****************************************************************/
template<typename HashedObj>
bool HashTable<HashedObj>::insert(const HashedObj & x)
{
    int currentPos = findPos(x);//先找到位置
    if(isActive(currentPos))//如果该位置处已经存放了该元素,则之间返回false
        return false;

    array[currentPos] = HashEntry(x, ACTIVE);

    //如果当前散列表中元素的个数大于散列表长度的一半,则扩大散列表为原来的2倍
    if(++currentSize > array.size()/2)
        rehash();//扩充表的大小

    return true;

}

4、删除函数remove():

remove删除函数使用的是懒惰删除,并不把散列表中的元素情况,仅仅是将标识位info设置为Deleted。

/****************************************************************
*   函数名称:remove(const HashedObj & x)
*   功能描述: 删除散列表中的值为x的元素 
*   参数列表: x数据项
*   返回结果:成功删除返回true,否则返回false
*****************************************************************/
template<typename HashedObj>
bool HashTable<HashedObj>::remove(const HashedObj & x)
{
    int currentPos = findPos(x);
    if(!isActive(currentPos))
        return false;

    array[currentPos].info = DELETED;//懒惰删除,仅仅将标识位info设置为Deleted

    --currentSize;
    return true;
}

定义数据类:


class Employee{
    public:
        Employee(){name=""; salary=0.0; seniority=0;};
        Employee(const string & n, double sal, int sen):name(n), salary(sal), seniority(sen){}
        //获得该类的name成员
        const string & getName() const
        { return name; }

        //重载==运算符 
        bool operator==(const Employee & rhs) const
        { return getName() == rhs.getName(); }

        //重载!=运算符 
        bool operator!=(const Employee & rhs) const
        { return !(*this == rhs); }

        friend ostream & operator<<(const ostream & os, const Employee & e){
            cout << "name: " << e.name << ",\tsalary: " << e.salary << ",\tseniority: " << e.seniority;
        }
    private:
        string name;
        double salary;
        int seniority;
};

将该类的对象插入到散列表中进行测试。在main函数可以看到。


下面是main函数,主要是对散列表类进行测试。


int main()
{
    Employee e1("linux", 101.00, 1);
    Employee e2("ever", 102.00, 2);
    Employee e3("peter", 103.00, 3);
    Employee e4("may", 104.00, 4);
    Employee e5("usa", 105.00, 5);
    Employee e6("sal", 106.00, 6);
    Employee e7("usa", 107.00, 7);//和上面的值重复,所以这个值会被忽略
    Employee e8("jan", 108.00, 8);
    Employee e9("kro", 109.00, 9);
    Employee e10("bei", 110.00, 10);
    
    Employee e12("bbb", 110.00, 10);

    vector<Employee> v;
    v.push_back(e1);
    v.push_back(e2);
    v.push_back(e3);
    v.push_back(e4);
    v.push_back(e5);
    v.push_back(e6);
    v.push_back(e7);
    v.push_back(e8);
    v.push_back(e9);
    v.push_back(e10);

    cout << "v: " << endl;
    for(unsigned i = 0; i < v.size(); ++i)
        cout << v[i] << endl;
    cout << endl;

    HashTable<Employee> hashTable;
    
    for(unsigned i = 0; i < v.size(); ++i)
        hashTable.insert(v[i]);

    hashTable.print();

    cout << endl;

    cout << "测试包含函数containes: " << endl; 
    if(hashTable.containes(e10))
        cout << "containe e10" << endl;
    else 
        cout << "not containe e10" << endl;

    if(hashTable.containes(e12))
        cout << "containe e12" << endl;
    else 
        cout << "not containe e12" << endl;
        
    cout << "\n测试findElement(): " << endl;
    
    Employee e11 = hashTable.findElement(e8);
    cout << "e11: " << e11 << endl;
    cout << endl;

    cout << "测试isEmpty(): " << endl;
    if(hashTable.isEmpty())
        cout << "hashTable is Empty " << endl;
    else
        cout << "hashTable is not Empty " << endl;
    cout << endl;

    cout << "测试makeEmpty(): " << endl;
    hashTable.makeEmpty();
    if(hashTable.isEmpty())
        cout << "hashTable is Empty " << endl << endl;
    else
        cout << "hashTable is not Empty " << endl;

    return 0;
}


下面是该散列表类的源代码:


/*************************************************************************
	> File Name: hashTable.cpp
	> Author: 
	> Mail: 
	> Created Time: 2016年04月12日 星期二 10时35分14秒
 ************************************************************************/

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;


/****************************************************************
*   数据类名称:Employee
*   内容描述: 作为散列表的数据项目
*****************************************************************/

class Employee{
    public:
        Employee(){name=""; salary=0.0; seniority=0;};
        Employee(const string & n, double sal, int sen):name(n), salary(sal), seniority(sen){}
        //获得该类的name成员
        const string & getName() const
        { return name; }

        //重载==运算符 
        bool operator==(const Employee & rhs) const
        { return getName() == rhs.getName(); }

        //重载!=运算符 
        bool operator!=(const Employee & rhs) const
        { return !(*this == rhs); }

        friend ostream & operator<<(const ostream & os, const Employee & e){
            cout << "name: " << e.name << ",\tsalary: " << e.salary << ",\tseniority: " << e.seniority;
        }
    private:
        string name;
        double salary;
        int seniority;
};

/****************************************************************
*   函数名称:hash(const HashedObj & key)
*   功能描述: 根据键值求个hash值,这个函数是根据一个特定的数学公式 
*   参数列表: key 数据项的键值 
*   返回结果:返回一个通过散列函数求得的值
*****************************************************************/
int hash(const string & key)
{
    int hashVal = 0;

    //用散列函数的那个公式求和
    for(int i = 0; i < key.length(); ++i)
        hashVal = 37*hashVal + key[i];

    return hashVal;
}

/****************************************************************
*   函数名称:hash(const HashedObj & key)
*   功能描述: 根据键值求个hash值,这个函数是根据一个特定的数学公式 
*   参数列表: key 数据项的键值 
*   返回结果:返回一个通过散列函数求得的值
*****************************************************************/
int hash(const Employee & item)
{
    return hash(item.getName());
}


/****************************************************************
*   散列表类名称:HashTable
*   内容描述: 散列表类
*****************************************************************/
template<typename HashedObj>
class HashTable{
    public:
        explicit HashTable(int size = 11):array(nextPrime(size)), currentSize(0){makeEmpty();}
        ~HashTable();

        bool containes(const HashedObj & x);//判断是否包含数据项x
        void makeEmpty();//清空散列表
        bool isEmpty();
        bool insert(const HashedObj & x);//插入项x
        bool remove(const HashedObj & x);//删除项x

        void print();//输出散列表中的内容
        int findPos(const HashedObj & x);//根据名字查找数据项
        HashedObj findElement(const HashedObj & x);//根据名字查找数据项,并返回

        enum EntryType {ACTIVE, EMPTY, DELETED};//每个数据单元都有一个info变量,表明该位置是否被占用、空或已删除
        
    private:
        //散列表的数据单元结构
        struct HashEntry{
            HashedObj element;//该散列表存放的数据
            EntryType info;//表明该位置的状态 

            HashEntry(const HashedObj & e = HashedObj(), EntryType i = EMPTY):element(e), info(i){}
        };

        vector<HashEntry> array;//散列表
        int currentSize;//散列表中当前存放的元素个数
    private:
        void rehash();//再散列
        int myhash(const HashedObj & x) const;//散列函数
        int nextPrime(int n);//求的距离N最近的一个大于N的素数
        int prePrime(int n);//求距离N最近的一个小于N的素数
        bool isActive(int currentPos) const;//判断位置currentPos处的是否有元素

    public:
        friend ostream & operator<<(const ostream & os, const HashEntry & e){
            cout << "element: " << e.element << ", info = " << e.info;
        }
};


/****************************************************************
*   函数名称:findElement(const HashedObj & x) const
*   功能描述: 查找x的位置
*   参数列表: x是要查找的元素
*   返回结果:如果找到则返回该元素的引用
*****************************************************************/
template<typename HashedObj>
HashedObj HashTable<HashedObj>::findElement(const HashedObj & x)
{
    int currentPos = findPos(x);

    if(isActive(currentPos))//找了则返回
        return array[currentPos].element;
    else{//没有找到,返回一个空值
        HashedObj obj;
        return obj;
    }
}


/****************************************************************
*   函数名称:findPos(const HashedObj & x) const
*   功能描述: 查找x应该插入的位置
*   参数列表: x是要插入的元素
*   返回结果:如果找到空的位置则返回要插入的位置标号
*****************************************************************/
template<typename HashedObj>
int HashTable<HashedObj>::findPos(const HashedObj & x)
{
    //线性探测f(i) = i; f(i) = f(i-1) + 1;相隔为1
    //平方探测f(i) = i*i; f(i) = f(i-1) + 2*i - 1; 相隔为2*i-1
    //双散列,f(i) = i*hash2(x); f(i) = f(i-1)+hash2(x);相隔为hash2(x);
    //hash2(x) = R-(x%R); R=prePrime(array.size()),R为小于TableSize()的素数
    int offset = 1;

    int currentPos = myhash(x);

    //如果找到了空的位置则返回位置标号
    //如果找到了该元素x,则返回该元素的位置标号
    while(array[currentPos].info != EMPTY && array[currentPos].element != x){
         //currentPos += offset;//线性探测
         currentPos += 2 * offset -1;//平方探测
         //currentPos += prePrime(array.size()) - myhash(x)%prePrime(array.size());//双散列
         offset++;

        if(currentPos >= array.size())
            currentPos -= array.size();
    }
    
    return currentPos;
}


/****************************************************************
*   函数名称:print()
*   功能描述: 输出散列表中的内容
*   参数列表: 无 
*   返回结果:无
*****************************************************************/
template<typename HashedObj>
void HashTable<HashedObj>::print()
{
    cout << "输出散列表中的内容: " << endl;
    for(unsigned i = 0; i < array.size(); ++i){
        if(isActive(i))
            cout << i << ": " << endl << array[i] << endl;
    }
}

/****************************************************************
*   函数名称:isEmpty()
*   功能描述: 判断散列表是否为空
*   参数列表: 无 
*   返回结果:无
*****************************************************************/
template<typename HashedObj>
bool HashTable<HashedObj>::isEmpty()
{
    return currentSize == 0;
}

/****************************************************************
*   函数名称:makeEmpty()
*   功能描述: 清空散列表 
*   参数列表: 无 
*   返回结果:无
*****************************************************************/
template<typename HashedObj>
void HashTable<HashedObj>::makeEmpty()
{
    for(int i = 0; i < array.size(); ++i)
        array[i].info = EMPTY;

    currentSize = 0;//当前元素个数设为0
}

/****************************************************************
*   函数名称:containes(const HashedObj & x) const
*   功能描述: 判断散列表是否包含值为x的元素 
*   参数列表: x数据项
*   返回结果:如果包括x则返回true,否则返回false
*****************************************************************/
template<typename HashedObj>
bool HashTable<HashedObj>::containes(const HashedObj & x)
{
    //findPos(x)返回的位置是ACTIVE的说明存在该元素x
    return isActive(findPos(x));
}

/****************************************************************
*   函数名称:isActive(int currentPos) const
*   功能描述: 判断位置currentPos处的是否有元素 
*   参数列表: currentPos是散列表currentPos处的位置 
*   返回结果:如果currentPos处有元素则返回true,否则返回false
*****************************************************************/
template<typename HashedObj>
bool HashTable<HashedObj>::isActive(int currentPos) const
{
    return array[currentPos].info == ACTIVE;
}

/****************************************************************
*   函数名称:remove(const HashedObj & x)
*   功能描述: 删除散列表中的值为x的元素 
*   参数列表: x数据项
*   返回结果:成功删除返回true,否则返回false
*****************************************************************/
template<typename HashedObj>
bool HashTable<HashedObj>::remove(const HashedObj & x)
{
    int currentPos = findPos(x);
    if(!isActive(currentPos))
        return false;

    array[currentPos].info = DELETED;//懒惰删除,仅仅将标识位info设置为Deleted

    --currentSize;
    return true;
}

/****************************************************************
*   函数名称:insert(const HashedObj & x)
*   功能描述: 在散列表中插入元素x,如果插入项已经存在,则什么都不做。
*             否则将其放在表的前端
*   参数列表: x数据项
*   返回结果:插入成功返回true, 否则返回false
*****************************************************************/
template<typename HashedObj>
bool HashTable<HashedObj>::insert(const HashedObj & x)
{
    int currentPos = findPos(x);//先找到位置
    if(isActive(currentPos))//如果该位置处已经存放了该元素,则之间返回false
        return false;

    array[currentPos] = HashEntry(x, ACTIVE);

    //如果当前散列表中元素的个数大于散列表长度的一半,则扩大散列表为原来的2倍
    if(++currentSize > array.size()/2)
        rehash();//扩充表的大小

    return true;

}


/****************************************************************
*   函数名称:~HashTable()
*   功能描述: 析构函数
*   参数列表: 无
*   返回结果:无
*****************************************************************/
template<typename HashedObj>
HashTable<HashedObj>::~HashTable()
{
    
}


/****************************************************************
*   函数名称:prePrime(int n)
*   功能描述: 获得距离n最近的一个小于n的素数 
*   参数列表: n表示数值 
*   返回结果:返回一个素数 
*****************************************************************/
template<typename HashedObj>
int HashTable<HashedObj>::prePrime(int n)
{
    int i;

    if(n % 2 == 0)
        n--;

    for(; ; n -= 2){
        for(i = 3; i*i <= n; i += 2){
            if(n % i == 0)
                goto ContOuter;
        }
        return n;

        ContOuter: ;
    }
}

/****************************************************************
*   函数名称:nextPrime(int n)
*   功能描述: 获得距离n最近的一个大于n的素数 
*   参数列表: n表示数值 
*   返回结果:返回一个素数 
*****************************************************************/
template<typename HashedObj>
int HashTable<HashedObj>::nextPrime(int n)
{
    int i;

    if(n % 2 == 0)
        n++;

    for(; ; n += 2){
        for(i = 3; i*i <= n; i += 2)
            if(n % i == 0)
                goto ContOuter;
        return n;
        ContOuter: ;
    }
}
/****************************************************************
*   函数名称:rehash()
*   功能描述: 扩大散列表的大小
*   参数列表: 无
*   返回结果:无 
*****************************************************************/
template<typename HashedObj>
void HashTable<HashedObj>::rehash()
{
    
    vector<HashEntry> oldArray = array;

    //创建一个新的大小为原来两倍大小的散列表
    array.resize(nextPrime(2 * oldArray.size()));

    for(int i = 0; i < array.size(); ++i)
        array[i].info = EMPTY;

    currentSize = 0;
    //复制散列表
    for(int i = 0; i < oldArray.size(); ++i){
        if(oldArray[i].info == ACTIVE)
            insert(oldArray[i].element);
    }
}

/****************************************************************
*   函数名称:myhash(const HashedObj & key)
*   功能描述: 根据键值求个hash值 
*   参数列表: key 数据项的键值 
*   返回结果:返回hash值
*****************************************************************/
template<typename HashedObj>
int HashTable<HashedObj>::myhash(const HashedObj & key) const
{
    int hashVal = hash(key);
    
    hashVal %= array.size();

    if(hashVal < 0)
        hashVal += array.size();

    return hashVal;
}




int main()
{
    Employee e1("linux", 101.00, 1);
    Employee e2("ever", 102.00, 2);
    Employee e3("peter", 103.00, 3);
    Employee e4("may", 104.00, 4);
    Employee e5("usa", 105.00, 5);
    Employee e6("sal", 106.00, 6);
    Employee e7("usa", 107.00, 7);//和上面的值重复,所以这个值会被忽略
    Employee e8("jan", 108.00, 8);
    Employee e9("kro", 109.00, 9);
    Employee e10("bei", 110.00, 10);
    
    Employee e12("bbb", 110.00, 10);

    vector<Employee> v;
    v.push_back(e1);
    v.push_back(e2);
    v.push_back(e3);
    v.push_back(e4);
    v.push_back(e5);
    v.push_back(e6);
    v.push_back(e7);
    v.push_back(e8);
    v.push_back(e9);
    v.push_back(e10);

    cout << "v: " << endl;
    for(unsigned i = 0; i < v.size(); ++i)
        cout << v[i] << endl;
    cout << endl;

    HashTable<Employee> hashTable;
    
    for(unsigned i = 0; i < v.size(); ++i)
        hashTable.insert(v[i]);

    hashTable.print();

    cout << endl;

    cout << "测试包含函数containes: " << endl; 
    if(hashTable.containes(e10))
        cout << "containe e10" << endl;
    else 
        cout << "not containe e10" << endl;

    if(hashTable.containes(e12))
        cout << "containe e12" << endl;
    else 
        cout << "not containe e12" << endl;
        
    cout << "\n测试findElement(): " << endl;
    
    Employee e11 = hashTable.findElement(e8);
    cout << "e11: " << e11 << endl;
    cout << endl;

    cout << "测试isEmpty(): " << endl;
    if(hashTable.isEmpty())
        cout << "hashTable is Empty " << endl;
    else
        cout << "hashTable is not Empty " << endl;
    cout << endl;

    cout << "测试makeEmpty(): " << endl;
    hashTable.makeEmpty();
    if(hashTable.isEmpty())
        cout << "hashTable is Empty " << endl << endl;
    else
        cout << "hashTable is not Empty " << endl;

    return 0;
}

程序输出结果:


v: 
name: linux,    salary: 101,    seniority: 1
name: ever,     salary: 102,    seniority: 2
name: peter,    salary: 103,    seniority: 3
name: may,      salary: 104,    seniority: 4
name: usa,      salary: 105,    seniority: 5
name: sal,      salary: 106,    seniority: 6
name: usa,      salary: 107,    seniority: 7
name: jan,      salary: 108,    seniority: 8
name: kro,      salary: 109,    seniority: 9
name: bei,      salary: 110,    seniority: 10

输出散列表中的内容: 
1: 
element: name: kro,     salary: 109,    seniority: 9, info = 0
3: 
element: name: jan,     salary: 108,    seniority: 8, info = 0
4: 
element: name: may,     salary: 104,    seniority: 4, info = 0
5: 
element: name: bei,     salary: 110,    seniority: 10, info = 0
6: 
element: name: usa,     salary: 105,    seniority: 5, info = 0
17: 
element: name: ever,    salary: 102,    seniority: 2, info = 0
18: 
element: name: sal,     salary: 106,    seniority: 6, info = 0
21: 
element: name: peter,   salary: 103,    seniority: 3, info = 0
22: 
element: name: linux,   salary: 101,    seniority: 1, info = 0

测试包含函数containes: 
containe e10
not containe e12

测试findElement(): 
e11: name: jan, salary: 108,    seniority: 8

测试isEmpty(): 
hashTable is not Empty 

测试makeEmpty(): 
hashTable is Empty 

  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值