我正在删除BucketSort类中的节点,但是如何删除对象?

时间:2018-11-16 01:24:08

标签: c++ sorting object destructor bucket

BucketSort::~BucketSort() {
    for (int i = 0; i < DEFAULT_CAPACITY; ++i) {
        if (a[i] != nullptr) {
            a[i]->deleteBucket();
        }
    }

    delete a;
    a = nullptr;
}

这是我的析构函数代码。我的教授告诉我,“通过调用deleteBucket函数,您将删除节点,而不是对象。”但是,我不明白这是什么意思,或者至少我必须忽略了一些东西。 我如何才能像她说的那样删除对象?

这基本上是一个存储桶排序类,它根据此处描述的存储桶排序算法进行排序:https://www.geeksforgeeks.org/wp-content/uploads/scene01801.jpg

所有这些类加起来就创建了。所有函数都可以正常工作,除了析构函数成员函数需要一些微调。

以下是存储桶排序的.cpp文件:

#include "bucketsort.h"

BucketSort::BucketSort() {
    a = new Bucket*[DEFAULT_CAPACITY]();
    numberOfElements = 0;
}

void BucketSort::print() const {
    for (int i = 0; i < DEFAULT_CAPACITY; ++i) {
        if (a[i] != nullptr) {
            a[i]->print();
        }
    }
}

void BucketSort::insert(const vector <double>& v)  {
    int index;

    int vectorSize = v.size(); // New addition to the codebase.
    for (int i = 0; i < vectorSize; ++i) {
        index = v[i] * 10;

        if (a[index] == nullptr) {
            Bucket* newBucket = new Bucket;
            a[index] = newBucket;
        }

        a[index]->insert(v[i]);
        ++numberOfElements;
    }
}

void BucketSort::createSortedList(Bucket& thisBucket) {
    for (int i = 0; i < DEFAULT_CAPACITY; ++i) {
        if (a[i] != nullptr && !a[i]->isEmpty()) {
            thisBucket.moveAppend(*a[i]);
        }
    }
}

BucketSort::~BucketSort() {
    for (int i = 0; i < DEFAULT_CAPACITY; ++i) {
        if (a[i] != nullptr) {
            a[i]->deleteBucket();
        }
    }

    delete a;
    a = nullptr;
}

这是我的标题供参考:

Bucketsort.h

#ifndef BUCKETSORT_H
#define BUCKETSORT_H    

#include "bucket.h"
#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

const int DEFAULT_CAPACITY = 10;

class BucketSort
{
public:
    // Constructors:
    BucketSort();
    // Functions:
    void print() const;
    void insert(const vector <double>& v);
    void createSortedList(Bucket& a);
    //
    ~BucketSort();
private:
    Bucket** a;
    int numberOfElements;
};

#endif

Bucket.h:

#ifndef BUCKET_H
#define BUCKET_H    

#include <iostream>
using namespace std;

class Node
{
public:
    Node() : item(0.0), link(nullptr) {}
    Node(double newItem, Node *newLink) : item(newItem), link(newLink) {}
    Node* getLink() const { return link; }
    double getItem() const { return item; }
    void setItem(double newItem) { item = newItem; }
    void setLink(Node *newLink) { link = newLink; }
    ~Node() {}
private:
    double item;
    Node *link;
};


class Bucket
{
public:
    Bucket();
    void insert(double value);
    void moveAppend(Bucket& otherBucket);
    void print() const;
    bool isEmpty() const;
    Bucket& operator=(Bucket&& otherBucket);
    void deleteBucket();
    ~Bucket();
private:
    Node * ptrToFirst;
    Node *ptrToLast;
    int numberOfElements;
};

#endif

SortedList.h:

#ifndef SORTEDLIST_H
#define SORTEDLIST_H    

#include "bucketsort.h"
#include <vector>

using namespace std;

class SortedList : public Bucket
{
public:
    SortedList();
    void sortList(const vector <double>& list);
    ~SortedList();
private:
};

#endif

测试驱动程序:

#include "Bucket.h"

void runBucketTest() {
    Bucket bucket1;
    Bucket bucket2;
    Bucket bucket3;
    Bucket bucket4;
    Bucket bucket5;
    Bucket bucket6;
    Bucket bucket7;
    Bucket bucket8;
    Bucket bucket9;

    cout << "--------------------------------------------" << endl;
    cout << "Bucket testing" << endl;
    cout << "--------------------------------------------" << endl;
    cout << "Bucket 1: ";
    bucket1.insert(77);
    bucket1.insert(42);
    bucket1.insert(93);
    bucket1.print();
    bucket2.insert(91);
    cout << endl << "Bucket 2: ";
    bucket2.print();
    bucket2.moveAppend(bucket1);
    cout << endl << "Bucket 2 after moveAppend w/bucket 1 having existing elements: ";
    bucket2.print();
    bucket4.insert(69);
    bucket4.insert(21);
    bucket4.insert(979);
    cout << endl << "Bucket 4: ";
    bucket4.print(); 
    cout << endl << "Bucket 3 after moveAppend w/bucket 4 having NO existing elements: ";
    bucket3.moveAppend(bucket4);
    bucket3.print();
    cout << endl << "Bucket insertion into Bucket 5 and then deletion: " << endl;
    bucket5.insert(101);
    bucket5.insert(202);
    bucket5.insert(303);
    cout << "Before deletion: ";
    bucket5.print();
    cout << endl << "After deletion: ";
    bucket5.deleteBucket();
    bucket5.print(); 
    cout << endl << "Bucket 6 = Bucket 7 assignment operator: " << endl;
    bucket7.insert(10);
    bucket7.insert(20);
    bucket7.insert(5);
    cout << "Buckets 6 before assignment operator: ";
    bucket6.print();
    cout << endl << "Bucket 7 before assignment operator: ";
    bucket7.print();
    cout << endl << "Bucket 6 after assignment operator (Bucket 6 = Bucket 7): ";
    bucket6 = move(bucket7);
    bucket6.print();
    bucket8.insert(22);
    bucket8.insert(21);
    cout << endl << "Bucket 8 (already containing some elements) before assignment operator: ";
    bucket8.print();
    cout << endl << "Bucket 8 after assignment operator (Bucket 8 = Bucket 9): ";
    bucket8 = move(bucket9);
    bucket8.print();
}

测试结果: https://i.imgur.com/zU7T2Su.png

我可以寻求帮助吗?

1 个答案:

答案 0 :(得分:0)

自从您实例化了Bucket指针数组

a = new Bucket*[DEFAULT_CAPACITY]();

您将需要删除每个指针所指的内存:

delete a[i];

应该导致的结果:

BucketSort::~BucketSort() {
    for (int i = 0; i < DEFAULT_CAPACITY; ++i) {
        if (a[i] != nullptr) {
            a[i]->deleteBucket();
            delete a[i];
        }
    }

    delete a;
    a = nullptr;
}