Having started C++ programming two days ago and never reading C++ before, I have no idea what my code should look like. I do have 5 years experience in Python and so am no stranger to liked lists. I just want to know if this would be considered good C++ code. It compiles perfectly on Mingw g++, and I have run basic tests on the functions. All I want is a code review. Any and all critique is both wanted and welcome. Thanks in advance :)
<List.h>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
struct node
{
node *next, *prev;
int i;
};
class List
{
node *start, *end;
public:
List();
intoperator[](int pos);
void append(int i);
int pop();
void place(int i, int pos);
void del(int pos);
int length();
};
It is very bad coding style. I wonder did you indeed 5 years program in Python?! I looked through only the header
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
struct node
{
node *next, *prev;
int i;
};
class List
{
node *start, *end;
public:
List();
intoperator[](int pos);
void append(int i);
int pop();
void place(int i, int pos);
void del(int pos);
int length();
};
What is it style coding?! Also I do not understand why structure Node is not a nested struct of class List? Do clients have direct access to it? Also subscripting operator function shall have a const visavi. I advice to see the interface of the standard class std::list.
I can list multiple flaws in your code that would turn your hair grey from trying to find them. I was going to list them, until I saw this:
Script Coder wrote:
"Having started C++ programming two days ago and never reading C++ before, I have no idea what my code should look like."
Then why did you attempt to implement a linked list? While LLs are simple to understand, they are an absolute pain to implement and maintain, especially in C++. I recommend learning C++ before implementing a LL.
Python does not have structs, so its new for me, but i'll be sure to change it...thank you for the critique, anything else...also what would be a good project for me next, to improve my skill?
I can list multiple flaws in your code that would turn your hair grey from trying to find them
could you please list them, i learn most from my mistakes, that is why i attack hard challenges, so that i can make more mistakes and thus learn more :)
1) node lacks a constructor & destructor
2) List::operator[] isn't being used correctly. A user would expect a node.
3) There are no checks for NULL; a heap corruption and/or access violation waiting to happen.
4) List lacks a destructor. A fatal mistake.
5) There's no documentation at all. It's hard to determine what you intend to accomplish in each function.
Clearly, you're in desperate need of a book. Don't take this the wrong way, but you're not impressing anybody. 2 days is no where near enough time. In that time frame, you should be implementing basic programs, such as a calculator, or a shop till simulator. Python is far from C++.
could you please list them, i learn most from my mistakes, that is why i attack hard challenges, so that i can make more mistakes and thus learn more :)
For example how to determing that a list is empty? What iwill length() return for newly created list?!
Why does not Node have a constructor? There are many questions to your design of list.
Thank You guys for all your help. Just to defend myself a bit (i feel a bit attacked, maybe its just me) :
List::operator[] isn't being used correctly. A user would expect a node.
In my second post, I said that i would make node internal to List, therefore the user, will only know that it is a list of ints, they aren't able to touch anything of type "node", hence no NULL checks...i handle all the node code myself.
And sorry, i do have a hard time documenting my code, and also I did not know a struct could have a constructor and deconstructor. And could anyone suggest basic program ideas that involve maths, physics, db's, ect. that don't have GUIs
It's not just you. Vlad is a bit of a heated person. Just the way he is. Don't feel ashamed or anything. Like you said, you learn from mistakes. The best people to point out mistakes are the harsh critical ones. You just need to be tough enough to take the hits.
And Framework and I are just looking out for you. Learning by example and mistake is good. But you should really try learning as much as you can from books and tutorials before jumping into a hells problem.
The start node's i value stores the number of elements in the list
If you want to store the list size, you may want to store in the list. However that's not practical to maintain (check out splice)
Think what you will need to do if you want to store strings instead of numbers.
Edit: Using an "empty" node as a header it is not so bad idea, but then don't allocate it dynamically.
Framework wrote:
2) List::operator[] isn't being used correctly. A user would expect a node.
No, a user shouldn't know that node exists. He stores things in the list, and expect to receive that things.
How the list manage that (if it wraps on another structure) it's irrelevant.
Edit: You may want to edit the elements, so return a reference instead (for the non-const version). Still, using that operator to iterate through the list will be awful. O(n^2)
No, a user shouldn't know that node exists. He stores things in the list, and expect to receive that things.
How the list manage that (if it wraps on another structure) it's irrelevant.
But it is very relevant if he wants to learn how to properly code. What if he needs someone to change something for him?
Also. How do you guys get the person's name above the quote?
"No, a user shouldn't know that node exists. He stores things in the list, and expect to receive that things. How the list manage that (if it wraps on another structure) it's irrelevant."
No, judging by the use of node::i, he's using it to store the offset of the node. That said, List::operator[] returns the offset of a node. Surely, a user would expect data or a node, not a index. Look at an array. The sub-script operator returns a value from the array, not the element's offset.
Additionally, there's many potential access violations just waiting to happen.
Vlykarye wrote:
"How do you guys get the person's name above the quote?"
When you use quote tags, make sure the opening tag resembles this: [quote=Username].
node::i holds the data, there is no stored offset into the list. Could you guys please give simple example projects that have to do with math and physics that you feel would be appropriate?
I have altered my program, unfortunately I have not yet added comments yet. If there is an unclear piece of code I will explain it. Once again, all critique is wanted, no matter how harsh. Thanks in advance.
class List
{
struct node
{
node *next, *prev;
int i;
node(List::node* n, List::node *p, int i);
};
node *start, *end;
int size;
public:
List();
~List();
intoperator[](int pos);
void append(int i);
int pop();
void place(int i, int pos);
void del(int pos);
int length();
};