C++ 链表数组的析构函数

C++ 链表数组的析构函数,c++,hashtable,singly-linked-list,C++,Hashtable,Singly Linked List,我很难为我的哈希表类找到析构函数,析构函数如下: template <typename ElementType> HashSet<ElementType>::~HashSet() noexcept { for (unsigned int i=0;i<hashCapacity;i++) { Node* current = hashTable[i]; while(current != nullptr) {

我很难为我的哈希表类找到析构函数,析构函数如下:

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;
}
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;
            }
        }
    }
}
其中,我的add函数和默认构造函数实现如下所示:

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;
}
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;
            }
        }
    }
}
模板
HashSet::HashSet(HashFunction HashFunction)
:hashFunction{hashFunction}
{
hashCapacity=默认容量;
hashSize=0;
hashTable=新节点*[hashCapacity];
对于(int i=0;i 0.8)
{
}
其他的
{
无符号整数索引=hashFunction(元素)%hashCapacity;
hashSize+=1;
节点*添加=新节点;
添加->下一步=nullptr;
添加->值=元素;
if(哈希表[索引]==nullptr)
{
哈希表[索引]=添加;
}
其他的
{
Node*addNode=哈希表[索引];
while(addNode->next!=nullptr)
{
addNode=addNode->next;
}
addNode->next=添加;
}
}
}
}

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

first
delete[]entry是错误的。未分配阵列,不应删除阵列。第二,如果你认为
节点的析构函数不够重要,不能包含在这里,请再想一想。无论如何,你为什么要重新发明轮子,已经有了。旁注:布尔值通常由
if(condition)
或在否定变量
if(!condition)
检查。创建一个你需要使用的,但是如果没有任何
main()
函数,很难猜测这是否是segfault的来源。