Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

15. (15 pts) Write a deep copy constructor for the class List (dynamically linke

ID: 3730251 • Letter: 1

Question

15. (15 pts) Write a deep copy constructor for the class List (dynamically linked) that accepts a const reference to another List and does not return a value. Given: a class ListNode with the following private data members: ListNode mpNext; int mData; and the following public member functions: ListNode (int newData = 0); // getters int getData() const: ListlodegetNextPtr) const // setters void setData (const int newData) void setNextPtr (ListNode *const pNewNext) i and a class List with the following private data member: ListNode mpHead;

Explanation / Answer

ListNode::ListNode(const ListNode &old_obj){
   ListNode curr = mpNext;
   ListNode temp = old_obj;
   while(temp!=NULL){
       if(curr == NULL){
           mData = temp.Data;
           mpNext = new ListNode();
       }
       else{
           mpNext.mData = temp.mData;
           mpNext = new ListNode();
       }
       mpNext = mpNext->mpNext;
       temp = temp.mpNext;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote