Question: [C++] How to properly de-allocate memory from this linked list using a while loop? code: cout < < Input Values and Type 'exit' to end

[C++] How to properly de-allocate memory from this linked list using a while loop?

code:

cout << "Input Values and Type 'exit' to end the Program" << endl;

string s;

Node *head = new Node();

Node *temp = head;

while(1){

cin >> s;

if(s!= "exit"){

temp -> item = s;

temp -> next = new Node();

temp = temp -> next; }

else {break;}

}

temp -> next = nullptr;

temp = head;

cout << " ";

cout << "The values in the list are: " << endl;

cout << "---------------------------" << endl;

while (temp->next != nullptr){

cout << temp -> item << endl;

temp = temp -> next;

}

/*

Properly de-allocate linked list

*/

return 0;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!