Question3: (40 pts) In the first project, we will insert and remove function def
ID: 3883117 • Letter: Q
Question
Question3: (40 pts) In the first project, we will insert and remove function defined as . insert (prep tr value, element): means insert the element AFTER the data value showed in the linked list. remove (preptr_ value); means delete the element AFTER the data_value (NOTE!! Not delete the value itself!!) Assume we also have proper constructor). Empty() and print (). What is the expected output from the following main)? int main) PointerList sl new PointerList() //insert at the beginning sl->insert (0,10); sl->insert (10,20): sl->insert (20,30) sl->insert (20, 40) sl->print ); s1->insert (30, 50) sl->insert (50, 60): sl->print ); sl->remove (30); sl->remove (10) sl- remove (50); sl-sprint ); return 0;Explanation / Answer
In the given program there are two important functions; one is insert and another is remove.
Here insert function add an element in to the list after certain given data element of that list. This function takes two arguments; first argument is the data value AFTER which the new element should be inserted; second argument is the new element which is to be inserted into the list; when the first argument of this function is 0 then the new element should be inserted at the first position of the list.
Now the second function is remove and it takes only one argument; this argument indicates that data element which is AFTER the given argument value is to be deleted from the list.
Now we are able to trace the output from the given main function.
In the first line of the main function an empty linklist is created which is pointed by a pointer called sl.
Now sl->insert (0,10) : this means 10 is inserted into the list at the first position. After this line of execution the list would contain [10].
Next sl->insert (10,20) : this means 20 is inserted into the list after the data element 10. After this line of execution the list would contain [10, 20].
Next sl->insert (20,30) : this means 30 is inserted into the list after the data element 20. After this line of execution the list would contain [10, 20, 30].
Next sl->insert (20,40) : this means 40 is inserted into the list after the data element 20. After this line of execution the list would contain [10, 20, 40, 30].
In next line sl->print(): this statement would print the current elements present in the list and that would be : [10, 20, 40, 30].
Next sl->insert (30,50) : this means 50 is inserted into the list after the data element 30. After this line of execution the list would contain [10, 20, 40, 30, 50].
Next sl->insert (50,60) : this means 60 is inserted into the list after the data element 50. After this line of execution the list would contain [10, 20, 40, 30, 50, 60].
In next line sl->print(): this statement would print the current elements present in the list and that would be : [10, 20, 40, 30, 50, 60].
Now sl->remove(30): this statement would remove the element that is after the data element 30. In the current list 50 is the data element that is immediately after 30. So 50 would be deleted. After this line of execution the list would contain [10, 20, 40, 30, 60].
Next sl->remove(10): this statement would remove the element that is after the data element 10. In the current list 20 is the data element that is immediately after 10. So 20 would be deleted. After this line of execution the list would contain [10, 40, 30, 60].
Next sl->remove(50): this statement would remove the element that is after the data element 50. In the current list 50 is not present. So nothing would be deleted from current list. After this line of execution the list would contain [10, 40, 30, 60].
In next line sl->print(): this statement would print the current elements present in the list and that would be : [10, 40, 30, 60].
Now the main function ends. So there are three print statements in this program which will generate output in the console. And the expected output would be as follows:
[10, 20, 40, 30]. //due to first print statement
[10, 20, 40, 30, 50, 60]. //due to second print statement
[10, 40, 30, 60]. //due to last print statement
/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.