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
  • How do i write a destructor for this class? #ifndef NODE_H #define NODE_H #include <iostream> using...

    How do i write a destructor for this class? #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node{ public: // Constructor // Preconditions: None // Postconditions: None Node(); // Destructor // Preconditions: None // Postconditions: Frees dynamically allocated memory ~Node(); // Overloaded Constructor // Preconditions: None // Postconditions: Initializes member variable with given argument Node(bool value); // ReplaceValue - Replaces m_value with opposite (if true then false or if false then true) // Preconditions: None // Postconditions: m_value...

  • // 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++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

  • I'm having trouble getting this program to compile and run. It is in C++ Language and...

    I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be:       H create link at head       R remove link at head       T create link at tail       K remove link at tail       I remove link at ID          S search...

  • 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...

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