99.0k views
4 votes
Consider the following code which deletes all the nodes in a linked list. void doublyLinkedList::destroy() { nodeType *temp; //pointer to delete the node while (first != nullptr) { temp = first; first = first->next; ____ } last = nullptr; count = 0; } Which of the following is the missing statement?

1 Answer

3 votes

Answer:

The answer is "delete temp;".

Step-by-step explanation:

  • In the given code a class "doublyLinkedList" is defined that contains a method that is destroy(). This code uses a scope resolution operator to define the method outside the class.
  • To access this method write return type of function then class name:: (use scope resolution operator) and function name.
  • In this function, nodeType temp is a pointer variable that is used to delete the node.
  • In method definition, a loop is declared that check value that is first not equal to nullptr. which means the first node is not equal to null. In this loop, a temp variable holds the first variable value and the first variable value is holding another variable value and then use the "delete temp" statement to delete a node in the list.
  • Outside a loop, the last variable holds nullptr variable value and the count variable assigns a value that is 0.
User Fdelsert
by
5.5k points