Question
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
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
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