Regarding the class Node. The question states to,
"Write C++ statements that create the described linked chain. Beginning with a head pointer "headPtr" that contains "nullptr", create and attach a node for "C", then create and attach a node for "B", and finally create and attach a node for "A".
Assuming the class Node has already been declared and defined.
Assuming the member function "next" for class node sets the current node pointer to point to the next node in the linked-chain.
Is my code below correct?
1 2 3 4
Node<string>* headPtr;
Node<string>* node1 = new Node<string>("C", Node<string>* next);
Node<string>* node2 = new Node<string>("B", Node<string>* next);
Node<string>* node3 = new Node<string>("A", Node<string>* next);
Your code merely declares a pointer that has name 'headPtr'. There is no initialization in that statement.
The Node<string>* headPtr = nullptr; does initialize the headPtr with a known value (nullptr).
Btw, pointers do not contain anything. A pointer is a variable and a variable has a value.
I was informed that creating a new node, automatically links it to the previously created nodes,
By what miracle does it do that? On your line 4 there is only a call to the constructor of type Node<string>. How does the constructor function know about lists? Perhaps the list is a global object that all Nodes know about. Too bad, for then there can be only one list. If there is only one list and all Nodes know it, then the headPtr is unnecessary, for yau can always ask any Node object about the list.
There is only the Node type? No List type? No functions that could be used with Nodes (other than constructor and getNext)?
My guess is that this is what your program is supposed to look like.
1 2 3 4 5 6 7
Node<string>* headPtr=new Node<string>("C");
Node<string>* node2 = new Node<string>("B");
Node<string>* node3 = new Node<string>("A");
headPtr->next(node2);
node2->next(node3);
Doesn't make much sense for the head node to not contain a value. This is a linked list implementation, don't call it linked chain because it sounds really redundant. Also I understand that the class is already defined but this is definitely a poorly implemented list class.