Question
Given the definitions of the classes Node and List below, define the member function insert_after and the destructor. The member function insert_after passes in the
Given the definitions of the classes Node and List below, define the member function insert_after and the destructor.
The member function insert_after passes in the address to a Node (Node pointer) currently in the list and an integer value to be inserted into the list. This function should then create a new Node containing this integer value passed in and insert it into the list after the Node whose address is passed in. If the address passed in is 0, then insert the new Node at the front of the List.
The insert_after function is a private helper function that will be used within the insert_sorted public member function. You DO NOT need to define the insert_sorted function.
The destructor should ensure there are no memory leaks.
Notice there are no push_front, push_back or pop_front functions declared or defined for this List class. You may NOT define these or any other member functions, even as private helper functions.
struct Node {
public: Node *next; int data; Node(int val)
: next(0), data(val) {}
};
class List {
private: Node *head; Node *tail;
public: List() : head(0), tail(0) {} ~List(); //IMPLEMENT void insert_sorted(int); //DON'T IMPLEMENT
private:
void insert_after(Node *, int); //IMPLEMENT
};
Step 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