##Design
Design
##Design
Design
Well to start with I would note you have not made a linked list. But, but a chain of Node(s). This may seem to be the same thing but I would see this as different as a linked list would encapsulate and hide its internal structure with a class and provide methods to access the data.
So you have:
I would adviceadvise it. It avoids a few corner cases where things can go wrong.
Sure you can write it like this.:
Your print only prints to std:std::cout
:cout.
It would be nice if it printed to any stream. Then you can print it to a string or a file. Even if the default is std::coutstd::cout
.
Also, the standard way to print things in C++ is to use operator<<
. So it would be nice if you wrote the appropriate operator.
Const correctness.
A big push in C++ is const correctness. This is marking parameters and members as const if they are not mutated/mutating. In the above code the print is not supposed to mutate the list. So it might be nice to mark the nodes as constconst
.
Well to start with I would note you have not made a linked list. But a chain of Node(s). This may seem to be the same thing but I would see this as different as a linked list would encapsulate and hide its internal structure with a class and provide methods to access the data.
So you have
I would advice it. It avoids a few corner cases where things can go wrong.
Sure you can write it like this.
Your print only prints to std::cout.
It would be nice if it printed to any stream. Then you can print it to a string or a file. Even if the default is std::cout.
Also the standard way to print things in C++ is to use operator<<
. So it would be nice if you wrote the appropriate operator.
Const correctness.
A big push in C++ is const correctness. This is marking parameters and members as const if they are not mutated/mutating. In the above code the print is not supposed to mutate the list. So it might be nice to mark the nodes as const.
I would note you have not made a linked list, but a chain of Node(s). This may seem to be the same thing but I would see this as different as a linked list would encapsulate and hide its internal structure with a class and provide methods to access the data.
So you have:
I would advise it. It avoids a few corner cases where things can go wrong.
Sure you can write it like this:
Your print only prints to std::cout
:
It would be nice if it printed to any stream. Then you can print it to a string or a file. Even if the default is std::cout
.
Also, the standard way to print things in C++ is to use operator<<
. So it would be nice if you wrote the appropriate operator.
Const correctness
A big push in C++ is const correctness. This is marking parameters and members as const if they are not mutated/mutating. In the above code the print is not supposed to mutate the list. So it might be nice to mark the nodes as const
.
- 3.6k
- 1
- 15
- 37
I would advice it. It avoids a few corner cases. were where things can go wrong.
This should be a private member of youyour linked list.
Also exposing a RAW pointer leads to memory management issues. Is this an owning RAW pointer or not? i.e. who is responsible for calling delete.?
void print_list(Node* list, std::ostream& str = std::countcout)
I would advice it. It avoids a few corner cases. were things can go wrong.
This should be a private member of you linked list.
Also exposing a RAW pointer leads to memory management issues. Is this an owning RAW pointer or not? i.e. who is responsible for calling delete.
void print_list(Node* list, std::ostream& str = std::count)
I would advice it. It avoids a few corner cases where things can go wrong.
This should be a private member of your linked list.
Also exposing a RAW pointer leads to memory management issues. Is this an owning RAW pointer or not? i.e. who is responsible for calling delete?
void print_list(Node* list, std::ostream& str = std::cout)