Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions

Question

7. Define cultural space.

Answered: 1 week ago

Question

8. Describe how cultural spaces are formed.

Answered: 1 week ago