Question

~DecayList() – The destructor de-allocates any dynamically allocated memory. How do I implement ~decaylist()? #ifndef DECAYLIST_H...

  • ~DecayList() – The destructor de-allocates any dynamically allocated memory.

How do I implement ~decaylist()?

#ifndef DECAYLIST_H

#define DECAYLIST_H

#include <iostream>

#include "Node.h"

using namespace std;

const int NUM_CONSECUTIVE = 3;

class DecayList{

public:

  // Constructor

  // Preconditions: None

  // Postconditions: New list is created

  DecayList();

and here is the node class.

  // Destructor

  // Preconditions: None

  // Postconditions: Dynamically allocated memory freed

  ~DecayList();

#include <iostream>

#include "Node.h"

using namespace std;

Node::Node(){

    m_next = NULL;

    m_value = NULL;

}

Node::~Node(){

    //delete the node pointer using the delete statement

    delete m_next;

    

}

Node::Node(bool value){

    m_next = NULL;

    m_value = value;

}

void Node::ReplaceValue(){

    m_value = !m_value;

}

void Node::SetNext(Node* next){

    m_next = next;

}

Node* Node::GetNext(){

    return m_next;

}

bool Node::GetValue(){

    return m_value;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Please find a short explaination on dynamic destruction below.

important points are highlighted.

If i have understood your question correctly,
given node class , which seems to be implementing each node of singly linked list.

Assuming each nodes is dynamically allocated memory .,
while you are inserting elements into the list,
you might be using below code, using new operation to allocate memory to each node.

//Allocate a new node.
Node *toBeInsertedNode = new (std::nothrow)Node(myvalue);

which would call constructor of Node class,

so m_next = NULL;
m_value = myvalue;

so when the whole class is destroyed, we need to make sure there are no memory leaks.
All the memory allocated has to be cleaned up.

since in your program ,
Node::~Node(){
//delete the node pointer using the delete statement
delete m_next;
  
}
destructor is already cleaning up next pointers, you can call Node destructor for each node in linkedlist as below.

so i will discuss a case which wont work, and a case which should be used as an alternative.

so considering you have a pointer to first item in the list,
Node* first;
DecayList():~DecayList()
{
   Node *myPointer = first; // take a pointer to head node
   while(myPointer)
   {
       head = myPointer->GetNext();
       delete myPointer; // High Lighted code
       myPointer = first;
   }
}

this will delete all elements in the list starting from head which we usually do, will be wrong.

consider what happens on highlighted code, when we delete a node, it will delete a node it is linked to.
and in turn that node will delete the node it is linked.
Its a chain reaction!.

which means that in above code of ours, mypointer will be a invalid pointer , pointing to invalid node.

so our destructor should just be like below.
DecayList():~DecayList()
{
   delete firstOrHead; // whatever you have given name in your program, first or head pointer.
}

using delete on a null pointer is perfectly fine.

kindly upvote.

Add a comment
Know the answer?
Add Answer to:
~DecayList() – The destructor de-allocates any dynamically allocated memory. How do I implement ~decaylist()? #ifndef DECAYLIST_H...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

  • C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

    C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...

  • This is a c++ class utilizing class templates and linked lists. I need to implement the...

    This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...

  • This is a c++ class utilizing class templates and linked lists and Nodes. I need to...

    This is a c++ class utilizing class templates and linked lists and Nodes. I need to implement the following member function(s) to LinkedBag.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. In this case, I need to join the original items with the user items(a_bag). So if the original has {1,2,3} and a_bag has...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • Im making a generic linked list. I cant figure out how to make an object of...

    Im making a generic linked list. I cant figure out how to make an object of my class from main. My 3 files are main.cpp dan.h dan.cpp The error is: 15 6 [Error] prototype for 'void ll<T>::insert()' does not match any in class 'll<T>' #include <iostream> #include <string> #include "dan.h" using namespace std; int main() { ll<int> work; work.insert(55);//THIS IS THE LINE THATS GIVING ME THE ERROR, Without this line it compiles and //runs. } #ifndef DAN_H #define DAN_H #include...

  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • above in the image is a linked list with 2 nodes. You can use this or...

    above in the image is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers C++ language Make sure its corect and runs without errors and I promise to give you a thumbs up. #include iostream» using namespace std; class Node int data;...

  • Need to implement this function, mostly confused withcopy constructor , operator overloading and deleteNode#ifndef TREE_H #define...

    Need to implement this function, mostly confused withcopy constructor , operator overloading and deleteNode#ifndef TREE_H #define TREE_H #include <iostream> #include <cstdlib> // necessary in order to use NULL class TreeNode { public: TreeNode() : left(NULL), right(NULL) {}    TreeNode* left; TreeNode* right; int value; }; class Tree { public: // Default constructor Tree();    // Copy constructor Tree(const Tree& other);    //Destructor ~Tree();    // overloaded Assignment Operator Tree& operator=(const Tree& other);    // Similar to insert function we discussed...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT