destructor for linkedlist

i need to create a destructor for this class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class LineEditor {
// class to model the functions of a line editor
    
public:
    
    LineEditor();
    // default constructor; sets list to empty
    
    virtual ~LineEditor();
    // destructor; deletes linked list
    
    
    bool   load(string file);
    // loads the data in the file given in the parameter to the linked list
    
    void   save();
    // saves the list to "default.txt"
    
    void   saveAs(string file);
    // saves the list to the file passed in the parameter
    
    
    void   showLines();
    // displays the lines in the list with line numbers
        
    void   insertLine(int lineNum, string newLine);
    // inserts a line at the given line number; if the line number is < 0, 
    // the line is inserted at 0; if the line number is >= the number of lines
    // in the list, the line is inserted as the last line in the list
    
    void   replaceLine(int lineNum, string replacementLine);
    // if the line number passed is valid, that line is replaced by the passed
    // line
    
    void   deleteLine(int lineNum);
    // if the line number passed is valid, that line is removed from the list


private:
    
    class LineNode {    // node to store a line
    public:
        string line;    // a single line
        LineNode *next; // pointer to the next line node
    };
    
    LineNode *lineList; // pointer to linked list of nodes containing lines
    int numLines;       // number of lines in the list stored in the linked list
    
    string filePath;    // name of file
};




1
2
3
4
LineEditor::~LineEditor() {
    // my code here
    
}
1
2
3
4
5
6
7
8
9
10
LineEditor::~LineEditor() {
    LineNode *current;
    LineNode *previous;
    current = lineList;
    while(current) {
        previous = current;
        current = current -> next;
        delete previous;
    }    
}
Topic archived. No new replies allowed.