Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer in C++ code, I don't know what I'm doing wrong. Question: Complete the function stringList that accepts a Node * (the head of

Please answer in C++ code, I don't know what I'm doing wrong.

Question:

Complete the function stringList that accepts a Node * (the head of a linked list) as a parameter and creates a string out the list in the following format:

Node 0: 100 -> Node 1: 200 -> Node 2: 300 If the list is empty then the function should return "Empty list".

This time we do not provide you with local test cases. You should write your own test cases. In main.cpp, you should test your stringList in, at least, the following cases:

An empty list A list with exactly one node A list with more than one node (eg: 100 -> 200 -> 300, as shown above)

test.h:

#ifndef TEST_H #define TEST_H

#include

using namespace std;

struct Node { int data_; Node *next_; };

string stringList(Node *n);

#endif

test.cpp:

#include "test.h" #include

using namespace std;

string stringList(Node *head) { int count = 0; Node* temp = head; if (head == NULL) { return "Empty list"; } else { while (temp != NULL) { count++; cout << temp->data_ << " "; temp = temp->next_; } } return "Node " + to_string(count) + ": " + to_string(temp->data_) + " -> "; }

main.cpp:

#include "test.h" #include using namespace std;

int main() { // Test 1: An empty list Node * head = NULL; cout << stringList(head) << endl;

// Test 2: A list with exactly one node // Test 3: A list with more than one node

return 0; }

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

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions

Question

=+e. Does it use simple language, not technical jargon?

Answered: 1 week ago

Question

Why does sin 2x + cos2x =1 ?

Answered: 1 week ago

Question

What are DNA and RNA and what is the difference between them?

Answered: 1 week ago

Question

Why do living creatures die? Can it be proved that they are reborn?

Answered: 1 week ago

Question

2. Write two or three of your greatest weaknesses.

Answered: 1 week ago