Hello,
I'm trying to create a linked list in c++, but its not working out too well. Any suggestions to where i'm going wrong would be great. Here's what i have so far:
You SLinkedList::add() is broken. Currently,you are creating a new temporary node on the stack. Next, you are allocating a new node on the heap. Then, you are assigning that new node pointer to be the original stack node (which gives you a memory leak). Also, once your function ends, your temporary stack node gets destructed, meaning your node pointer is now pointing at bad data.
FYI, your else if section could be changed to just a else.
First of, thanks for the quick reply.
The thing is, i'm new to c++ and still not used to pointers (i'm more of a JAVA programmer which does not include pointers in the language). I sort of understood your main point, but still can't seem to fix it. Any ideas?
If you are new to C++ my suggestion would be to learn how to use the std::list that is already provided by the language. Reinventing the wheel is not a good idea, especially if you are a beginner with C++. The std::list already provides built in algorithms and iterator support providing many useful features and I doubt that you would want to redo all of that. http://cplusplus.com/reference/stl/list/