Intro
Solving some problems with C++ is quite interesting:
This one, as simply as follows: You have a sequence of nodes, and you need to print the sequence of values within the nodes.
class Node
{
public:
int data;
Node* next;
Node(int data, Node* next = null)
{
this->data = data;
this->next = next;
}
};
So then the test is to create a function that returns, as string,”0 -> 1 -> 4 -> 9 -> 16 -> nullptr”, when this goes in new Node(0, new Node(1, new Node(4, new Node(9, new Node(16)))))
All my solutions are here [3] and are details below.
Solution 0 (used in all the solutions)
All my solutions use while(list) to iterate over the nodes, it’s quite simple and direct utilization. The vector used was from std [4] – as suggested. This article here [5] talks about the performance of STL vectors and specifically the push_back function.
My main solutions was (pseudo-algorithm)
1. Read the nodes
2. Accumulate the values
3. Read the accumulate values and create the string
4. Return the string
Solution 1
The solution one the one with everything combined. I run for each node, convert it to string, adding with the ” – > ” in the final string.
This solution is then an implementation of my pseudo-code in only one passage over the nodes. So:
Read node, create string, accumulate > Read node, create string, accumulate> Read node, create string, accumulate (…)
Solution 2
The solution one was the simple iterate on the nodes, create a vector of strings (vector library) and accumulate the strings. Later use a for loop to go on each and accumulate on the string the values. Adding the null at the end, since it has a comparison.
This solution is then a break on my pseudo-algorithm, in a way to break it in two:
1. Read, acc > read, acc > read, acc
2. read string, change, acc new string > read string, change, acc new string
Solution 3
This solution was basically the previous, but with the function += instead of append. This is nothing in C++ concatenation and append is different in Python for example.
Then the solution is the same as Solution 2
Solution 4
This solution I basically read the nodes, put all the info in a vector and then send this vector to another function, which aims to congregate all the string creation. This seems redundant but breaks the solution in two:
1. Reads the content of the nodes
list->data access.
2. Create the string
The function that will iterate over the temp vector and concatenate the string forming the ” -> ” value.
So then:
1. Read, acc > read, acc > read, acc (Scope function 1)
2. read string, change, acc new string > read string, change, acc new string (Scope function 2)
Notes
While doing some tests with Repl.it, my favourite online compilation framework, I came with a bizarre error related to the non declaration of add_list, I think is a memory problem on the previous runs, because the error wouldn’t go away. I deleted the solution, restarted and it worked – all good.
string add_list = nullptr;
for(int i = 0; i < temp.size(); i++){
add_list += temp[i];
add_list += “->”;
}
add_list += “->nullptr”;
Conclusion
My conclusion is to use the function or a separate solution, instead of putting everything together in the same place.
One must be careful because not all the issues that occur on the pointers access are segmentation fault[1], which can also occur. The solution with if(pointer) and while(pointer) are valid and are prefered over the pointer!=null actually, to simplify the reading of the code [2].
I would like to compare the solutions with other compilers and I found that nullptr only works if we have C11 and use std=c++0x, for Gcc 4.6 btw!!
REFs
[2]https://stackoverflow.com/questions/17772103/can-i-use-if-pointer-instead-of-if-pointer-null
[3] https://github.com/FranciscoMeloJr/C-Tests/tree/master/solutions_print
[4] https://en.cppreference.com/w/cpp/container/vector