删除LinkedList内容(析构函数)

时间:2016-07-14 19:40:49

标签: c++ destructor

我有一个关于我的析构函数如何在下面的代码中调用的快速问题。我有~LinkedList()析构函数调用自定义clear()函数来释放动态内存。

所以,如果我要删除LinkedList ll_one,我应该怎么做(在下面的source.cpp中)?类似下面的输出:

  

输出一个链接列表...

     

链接列表1 {19,18,17,16,15}

     

析构函数叫

     

输出一个链接列表...

     

Linked List one {}

//Node.h
#ifndef NODE
#define NODE
struct Node {
    int value;
    Node* next;

    Node(int i) : value(i), next(nullptr) {}
    Node() : value(0), next(nullptr) {}
};
#endif /*NODE*/
//LinkedList.h
#ifndef LINKEDLIST
#define LINKEDLIST
#include <ostream>
#include "Node.h"

class LinkedList {
    Node* head;
    Node* tail;
    std::string name;

    void recursePrint(Node*, std::ostream&) const;

public:
    LinkedList();
    LinkedList(std::string);
    // destrcutor declaration
    ~LinkedList();
    void setName(std::string);
    std::string getName() const;

    void insertFront(int);
    void insertBack(int);
    void clear();
    void print() const;
    void print(std::ostream&) const;
    void printReverse() const;
    void printReverse(std::ostream&) const;

    friend std::ostream& operator<<(std::ostream&, const LinkedList&);

};
#endif /*LINKEDLIST*/
//LinkedList.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;

LinkedList::LinkedList() : head(nullptr), tail(nullptr) {}
LinkedList::LinkedList(string name) : 
    head(nullptr), tail(nullptr), name(name) {}

// Define destructor
LinkedList::~LinkedList() {
    cout << "caling destructor" << endl;
    clear();
}

void LinkedList::setName(string name) {
    this->name = name;
}

string LinkedList::getName() const{
    return name;
}

void LinkedList::insertFront(int value) {
    Node* newNode = new Node(value);
    newNode->next = head; // attach to list
    head = newNode;
    if (tail == nullptr) { // empty list
        tail = newNode; // only node is both head and tail
    }
}

void LinkedList::insertBack(int value) {
    Node* newNode = new Node(value);
    if (tail != nullptr)
        tail->next = newNode; // attach to list
    tail = newNode;
    if (head == nullptr) { // empty list
        head = newNode; // only node is both head and tail
    }
}

// Define
void LinkedList::clear() {
    Node* temp;
    while (head != NULL) {
        temp = head->next;
        delete head;
        head = temp;
    }
    head = tail = NULL;


}

void LinkedList::print() const {
    print(cout);
}

void LinkedList::print(ostream& os) const {
    os << this;
}

void LinkedList::printReverse() const {
    printReverse(cout);
}

void LinkedList::printReverse(ostream& os) const {
    os << this->name << ": ";
    Node* current = this->head;
    if (current == nullptr) {
        os << "<Empty List>";
    }
    else
        recursePrint(current, os);
    os << endl;
}

void LinkedList::recursePrint(Node* node, ostream& os) const {
    if (node != nullptr) {
        recursePrint(node->next, os);
        os << node->value << " ";
    }
}

ostream& operator<<(ostream& os, const LinkedList& ll) {
    os << ll.getName() << " {";
    Node* current = ll.head;
    if (current == nullptr) {
        os << " <Empty List>";
    }
    while (current != nullptr) {
        if (current != ll.head)
            cout << ",";
        cout << " " << current->value;
        current = current->next;
    }
    cout << " }";
    return os;
}
//Source.cpp
#include <iostream>
#include "LinkedList.h"
using namespace std;

int main() {
    LinkedList ll_one("Linked List one");
    int num = 0;
    for (unsigned int i = 15; i < 20; ++i) {
        ll_one.insertFront(i);
    }
    cout << "Outputting Linked List one..." << endl;
    cout << ll_one << endl;

    // How to delete the ll_one?
    cout << "Outputting Linked List one..." << endl;
    cout << ll_one << endl;

    system("pause");
}

1 个答案:

答案 0 :(得分:0)

ll_one在堆栈上创建。当该对象离开main的范围时,它将自动弹出,并将调用其析构函数。如果在堆上分配它,则只需手动调用delete。

以下是在堆上分配的示例。

LinkedList* ll_one = new LinkedList();
delete ll_one;

如果您要清空列表,或者在删除列表之前释放它正在使用的任何空间,那么您将调用clear。

LinkedList* ll_one = new LinkedList();
ll_one->clear();
delete ll_one;

堆栈

LinkedList ll_one;
ll_one.clear();