Just answer the second question please. Just the code. Consider the two linked l
ID: 3672882 • Letter: J
Question
Just answer the second question please. Just the code.
Consider the two linked lists explained in lecture 10, slide 4: lstl and lst2. lst1 (list head 18 35 lst2 (list head 94 Ifwe try to assign lst2 to lstl, we will run into problems because lst2 will point only to the head of lstl (because of the way the linked list class is defined.) lst2 = 1st 1 ; lstl (list head 18 35 0 lst2 list head 94 0 We have 2 problems, First: ifwe call lst1.pop_front); lst2 will point to ull. Second, the elements 72 and 94 are wasted (shown in slide 5 of lecture 10) lstl list head 35 0 lst2 list head 94 (Continues in the next page)Explanation / Answer
linklist& linklist::operator=(linklist &list)
{
while (first !=NULL) // first empty list
deleteitem();
link* current=list.first;
while(current != NULL) // until ptr points beyond last link
{
additem (current->data); // print data
current=current->next; // move to next link
}
first=current;
return *this;
}
linklist l1;
l1.additem(72);
l1.additem(94);
l1.additem(0);
l1=l1;
linklist& linklist::operator=(linklist &list)
{
while (first !=NULL) // first empty list
deleteitem();
link* current=list.first;
while(current != NULL) // until ptr points beyond last link
{
additem (current->data); // print data
current=current->next; // move to next link
}
first=current;
return *this;
}
linklist l1;
l1.additem(72);
l1.additem(94);
l1.additem(0);
l1=l1;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.