Question
I've created a do while loop which takes in a number of values and stores them in a linked list. The problem is I want
I've created a do while loop which takes in a number of values and stores them in a linked list. The problem is I want the user to type in 'done' when they are done inputing, but when the loop ends the word 'done' winds up getting stored in the linked list. How do i end the loop without actually storing the ending loop case word?
Code:
struct Node {
string item;
Node *next;
};
int main(){
cout << "Automated Linked List / Enter Five Values" << endl;
cout << "------------------------------------------" << endl;
string s;
Node *head = new Node(); // head is always first in the list
Node *temp = head; // temp will iterate through list
do{
cin >> s;
temp -> item = s;
temp -> next = new Node();
temp = temp -> next; } while (s!="done");
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; // temp gets a new address to the next address which points to the next node in list
}
return 0;
}
ex:
orange
green
blue
done
The values in the list are:
orange
green
blue
done // I do not want this value stored
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started