Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(C++)Write a destructor for the singly-linked list. class Playlist is the class representing the linked list. class Node is a private inner class in Playlist
(C++)Write a destructor for the singly-linked list. class Playlist is the class representing the linked list. class Node is a private inner class in Playlist representing individual nodes.
class Playlist instances:
private class Node instances(Song_Entry is an object in each node)
Since your Playlist constructor is going to allocate memory on the heap, you must have a destructor or you will suffer memory leaks. Implement Playlist::~Playlist (); Make sure your destructor clears out the linked list by freeing all downstream nodes first and then deleting _head (which got allocated in the constructor). To accomplish this you would delete _head, which should trigger Playlist::-Node, the Node destructor. However, once invoked, the Node destructor will be called, and it should iteratively peel of one node at a time (adjacent to _head) and delete (free) it, When all downstream nodes have been released thus, the Node destructor should delete_head and return. Note that there is no guarantee a memory location won't be overwritten the moment you delete the object to which it belongs. Thus, before you delete a node's descendants (starting at its _next member), make sure that you have taken a copy of that member into your local variable. Don't try to access it AFTER the node has been deleted. Then you will likely encounter unpredictable errors that cause your code to work sometimes and not other times. Carelessness here usually results in forum posts that go "I'm sure I had it working, but something must have changed on the testing site because it's not working any more." Also, keep in mind that you should not delete the "this" object. It is the deletion of the current object that triggers its destructor to be called. In here, you should only delete downstream nodes. This paragraph will likely save you several hours of debugging frustration: When you destroy a node, you will have to delete its _next member. This means that a node's deletion will auto-invoke the delete on its_next pointer and so on. You want to make sure you delete no more than you should. Therefore make sure to (1) set every pointer you delete to nullptrso you can check before you attempt to double-delete something and (2) clear out a Node's next pointer (set to null) before you delete it. This will prevent the delete from cascading to affect all downstream nodes. private: Node *_head, *_tail, *_prev_to_current; int _size; class Node { private: Song_Entry _song; Node *_next; Since your Playlist constructor is going to allocate memory on the heap, you must have a destructor or you will suffer memory leaks. Implement Playlist::~Playlist (); Make sure your destructor clears out the linked list by freeing all downstream nodes first and then deleting _head (which got allocated in the constructor). To accomplish this you would delete _head, which should trigger Playlist::-Node, the Node destructor. However, once invoked, the Node destructor will be called, and it should iteratively peel of one node at a time (adjacent to _head) and delete (free) it, When all downstream nodes have been released thus, the Node destructor should delete_head and return. Note that there is no guarantee a memory location won't be overwritten the moment you delete the object to which it belongs. Thus, before you delete a node's descendants (starting at its _next member), make sure that you have taken a copy of that member into your local variable. Don't try to access it AFTER the node has been deleted. Then you will likely encounter unpredictable errors that cause your code to work sometimes and not other times. Carelessness here usually results in forum posts that go "I'm sure I had it working, but something must have changed on the testing site because it's not working any more." Also, keep in mind that you should not delete the "this" object. It is the deletion of the current object that triggers its destructor to be called. In here, you should only delete downstream nodes. This paragraph will likely save you several hours of debugging frustration: When you destroy a node, you will have to delete its _next member. This means that a node's deletion will auto-invoke the delete on its_next pointer and so on. You want to make sure you delete no more than you should. Therefore make sure to (1) set every pointer you delete to nullptrso you can check before you attempt to double-delete something and (2) clear out a Node's next pointer (set to null) before you delete it. This will prevent the delete from cascading to affect all downstream nodes. private: Node *_head, *_tail, *_prev_to_current; int _size; class Node { private: Song_Entry _song; Node *_nextStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started