链表数组的析构函数

问题描述

我在为我的 hashTable 类找出析构函数时遇到了麻烦,析构函数是这样的:

template <typename ElementType>
HashSet<ElementType>::~HashSet() noexcept
{
    for (unsigned int i=0;i<hashCapacity;i++)
    {
        Node* current = hashTable[i];
        while(current != nullptr)
        {
            Node* entry = current;
            current = current->next;
            delete[] entry;
        }
    }
    delete[] hashTable;
}

无论我使用 delete[] 还是 delete,它都会给我带来双重释放错误或分段错误

类模板如下:

template <typename ElementType>
class HashSet : public Set<ElementType>
{
public:
    // The default capacity of the HashSet before anything has been
    // added to it.
    static constexpr unsigned int DEFAULT_CAPACITY = 10;

    // A HashFunction is a function that takes a reference to a const
    // ElementType and returns an unsigned int.
    using HashFunction = std::function<unsigned int(const ElementType&)>;

public:
    // Initializes a HashSet to be empty so that it will use the given
    // hash function whenever it needs to hash an element.
    explicit HashSet(HashFunction hashFunction);

    // Cleans up the HashSet so that it leaks no memory.
    ~HashSet() noexcept override;

    // add() adds an element to the set.  If the element is already in the set,// this function has no effect.  This function triggers a resizing of the
    // array when the ratio of size to capacity would exceed 0.8,in which case
    // the new capacity should be determined by this formula:
    //
    //     capacity * 2 + 1
    //
    // In the case where the array is resized,this function runs in linear
    // time (with respect to the number of elements,assuming a good hash
    // function); otherwise,it runs in constant time (again,assuming a good
    // hash function).  The amortized running time is also constant.
    void add(const ElementType& element) override;

我的 add 函数认构造函数实现是这样的:

template <typename ElementType>
HashSet<ElementType>::HashSet(HashFunction hashFunction)
    : hashFunction{hashFunction}
{
    hashCapacity = DEFAULT_CAPACITY;
    hashSize = 0;
    hashTable = new Node* [hashCapacity];
    for (int i=0;i<hashCapacity;++i)
    {
        hashTable[i] = nullptr;
    }
}

template <typename ElementType>
void HashSet<ElementType>::add(const ElementType& element)
{
    if (contains(element)==false)
    {
        if ((hashSize/hashCapacity) > 0.8)
        {

        }
        else 
        {
            unsigned int index = hashFunction(element) % hashCapacity;
            hashSize += 1;
            Node* add = new Node;
            add->next = nullptr;
            add->value = element;
            if (hashTable[index]==nullptr)
            {
                hashTable[index] = add;
            }
            else
            {
                Node* addNode = hashTable[index];
                while(addNode->next != nullptr)
                {
                    addNode = addNode->next;
                }
                addNode->next = add;
            }
        }
    }
}

注意:调整哈希表大小的部分是不完整的,因为我正在检查哈希表的功能,以便首先保存少量值。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)