The first call of appendChild work as expected, but then the next point to itself. need an example how to do it.
why LinkNode nextNode; not create a new instance
zsbd zsbd zsbd zsbd zsbd zsbd
#include "iostream"
using namespace std;
template<typename T>
struct LinkNode {
T data;
bool hasNext = false;
LinkNode<T> *next = nullptr;
};
template<typename T>
void appendChild(LinkNode<T> &initNode, T &node) {
LinkNode<T> *ptr = &initNode;
while (ptr && ptr->hasNext) {
ptr = ptr->next;
}
// end of the chain
ptr->data = node;
ptr->hasNext = true;
// create new node
LinkNode<T> nextNode;
// append to the end
ptr->next = &nextNode;
cout << "done" << endl;
};
int main() {
int a = 123;
int b = 456;
int c = 789;
LinkNode<int> initNode;
initNode.data = 1;
appendChild(initNode, a);
appendChild(initNode, b);
appendChild(initNode, c);
cout << "finish" << endl;
}
Detail on the pause.