线性探查法实现的散列表(哈希表)类

const int DefaultSize = 100;
enum KindofStatus {Active, Empty, Deleted};

template<class E, class K>
class HashTable
{
    public:
        HashTable(int d, int sz = DefaultSize);
        ~HashTable() {delete []ht_; delete []info;}
        HashTable<E, K>& operator = (const HashTable<E, K>& rhs);
        bool Search(const K k1, const E& el) const;
        bool Insert(const E& el);
        bool Remove(const K k1, const E& el);
        void makeEmpty();
    private:
        int divitor_;
        int current_size_;
        int table_size_;
        E* ht_;
        KindofStatus* info;
        int find_pos(const K k1) const;
        int operator == (E& el) {return *this == el;}
        int operator != (E& el) {return *this != el;}
};

template<class E, class K>
int HashTable<E, K>::find_pos(const K k1) const
{
    int i = k1%divitor_;
    int j = i;
    do
    {
        if(info[j] == Empty || info[j] == Active && ht_[j] == k1)
            return j;
        j = (j + 1) % table_size_;
    }while(j !=i);
    return j;
}

template<class E, class K>
bool HashTable<E, K>::Search(const K k1, const E& el) const
{
    int i = find_pos(k1);
    if(info[i] != Active || ht_[i] != k1)
        return false;
    el = ht_[i];
    return true;
}

template<class E, class K>
void HashTable<E, K>::makeEmpty()
{
    for(int i=0; i<table_size_; i++)
    {
        info[i] = Empty;
        current_size_ = 0;
    }
}

template<class E, class K>
bool HashTable<E, K>::Insert(const E& el)
{
    K k1 = el;  // overload
    int i = find_pos(k1);
    if(info[i] != Active)
    {
        ht_[i] = el;
        current_size_ ++;
        info[i]++;
        return true;
    }
    if(info[i] == Active && ht_[i] == k1)
    {
        cout<<"this key has been contained"<<endl;
        return false;
    }
    cout<<"the table is full and can not be insert!"<<endl;
    return false;
}

template<class E, class K>
HashTable<E, K>& HashTable<E, K>::operator = (const HashTable<E, K>& rhs)
{
    if( *this != rhs)
    {
        delete []ht_;
        delete []info;
        table_size_ = rhs.table_size_;
        divitor_ = rhs.divitor_;
        current_size_ = rhs.current_size_;
        ht_ = new E[table_size_];
        info = new KindofStatus[table_size_];
        for(int i=0; i<table_size_; i++)
        {
            ht_[i] = rhs.ht_[i];
            info[i] = rhs.info[i];
        }

    }
    return *this;
}

template<class E, class K>
bool HashTable<E, K>::Remove(const K k1, const E& el)
{
    int i = find_pos(k1);
    if(info[i] == Active && ht_[i] == k1)
    {
        info[i] = Deleted;
        current_size_--;
        return true;
    }
    return false;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的线性探测实现散列表查找的 C 语言代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define TABLE_SIZE 10 typedef struct { int key; char value[20]; } HashNode; HashNode *hashTable[TABLE_SIZE]; int hash(int key) { return key % TABLE_SIZE; } void insert(int key, char *value) { int index = hash(key); while (hashTable[index] != NULL && hashTable[index]->key != key) { index = (index + 1) % TABLE_SIZE; } if (hashTable[index] != NULL) { free(hashTable[index]); } hashTable[index] = (HashNode *) malloc(sizeof(HashNode)); hashTable[index]->key = key; strcpy(hashTable[index]->value, value); } char *find(int key) { int index = hash(key); while (hashTable[index] != NULL && hashTable[index]->key != key) { index = (index + 1) % TABLE_SIZE; } if (hashTable[index] != NULL) { return hashTable[index]->value; } else { return NULL; } } int main() { insert(1, "apple"); insert(2, "banana"); insert(3, "cherry"); char *result = find(2); if (result != NULL) { printf("Key: %d, Value: %s\n", 2, result); } else { printf("Key not found: %d\n", 2); } return 0; } ``` 在上面的代码中,`HashNode` 结构体散列表中的一个节点,包括一个键和一个值。`hashTable` 数组是一个指针数组,每个指针指向一个 `HashNode` 结构体。`TABLE_SIZE` 宏定义了散列表的大小。`hash` 函数计算键的哈值。`insert` 函数使用线性探测将节点插入散列表中。`find` 函数使用线性探测查找键对应的值。在 `main` 函数中,我们插入了三个节点,并使用 `find` 函数查找键为 2 的节点的值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值