to my class, should it be a public or private member?? Also, once I have done that and changed length's type to size_type as well as the return type of the size() member function, how would you rectify the following line:
for (int i = 0; i < length; i++)
With this:
for (int i = 0; size_type(i) < length; i++)
This:
for (size_type i = 0; i < length; i++)
Or this:
for (int i = 0; i < int(length); i++)
I'm still not really sure of the point of making the variable an unsigned int. I understand that unsigned holds only positive values, however, length is a private member variable and is initialized to zero and is only acted upon from within the class definition.
The question of why is a good one. It's a question of semantics. In this situation, you could probably get away with working with an int because your code is fairly simple and you're the only one working with it. However, work on a large production system with 100 programmers and you might not be intimate with all the details. The more care you put into picking and designing data types, the more robust your systems will be and the easier it will be to debug problems in the future. It's best to get into that habit now with your simple programs.
On that note, I would also change the type of changeFlag to a bool. Making it a bool immediately shows the reader / maintainer that you are looking for a true/false value. Leaving it an int makes the reader / maintainer read the entire code block to interpret how it is used. Again, not a problem on a small project like this one, but it can come back to "byte" you on bigger projects.
> I'm still not really sure of the point of making the variable an unsigned int.
There is no point.
Though making it a std::size_t would be pedantically correct (the theoretical maximum number of nodes that the list can hold may be larger than the maximum value of an unsignedint).
> I understand that unsigned holds only positive values
The value that an unsignedint holds is interpreted as a non-negative value.
1 2 3 4 5 6 7 8 9
#include <iostream>
int main ()
{
unsignedint u = -1U ;
std::cout << u << '\n' ;
int i = u ;
std::cout << i << '\n' ;
}
So, for( int i = 0 ; i < 25 ; ++i ) { /* .... */ }
And not for( unsignedint i = 0 ; i < 25U ; ++i ) { /* .... */ }
That the range of the loop involves only non-negative values of i is irrelevant.
1 2 3 4 5
unsignedint just_cant_be_negative ;
std::cout << "please enter a non-negative integer: " ;
std::cin >> just_cant_be_negative ;
// did the user type in a number with a minus sign (like -23)?
// we just have no way of knowing.
Unless it is a library defined type like std::size_t, std::time_t, std::string::size_type, std::uint32_t (or you want to perform bit-wise operations), just use a plain int as the default integer type.
1. allocate an array of Node pointers of size length
2. set each pointer in the array to the corresponding list node
3. sort the pointers (using qsort - whatever) - you can access the node values through these pointers
4. reseat all the list node pointers by running along the array
5. delete the helper array
This will be much faster than a bubble sort
RE unsigned int for indexing into arrays - there is an awkward case where you want to scan an array from end to beginning with a simple for loop
for(unsignedint i = length; i>=0 ; --i) // oh-oh condition is always true
in this case the domain of the index is OK for the array but not for the loop condition