Linked-Based Implementations of ADT Bag

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);
Last edited on
I don't think so.

Beginning with a head pointer "headPtr" that contains "nullptr"

What does your headPtr contain?

Assuming the member function "next" for class node sets the current node pointer to point to the next node in the linked-chain.

What is a "current node pointer"?

Functions are called. You don't call "member function next" anywhere.


Do you have the "declaration" of call Node? (It is officially type definition, not declaration.)

Syntactically, the Node<string>* next cannot be an initializer for an object.

The headPtr is initialized to contain nullptr.

So in my understanding intializing it as Node<string>* headPtr is the same as doing it in this form, Node<string>* headPtr = nullptr; correct?

The 2nd half of the instructions says to create and attach a node for "C", "B", and "A".

I was informed that creating a new node, automatically links it to the previously created nodes, thereby creating a linked-chain.

So my updated version looks like this,
1
2
3
4
5
6
7
 
Node<string>* headPtr;
Node<string>* node1 = new Node<string>("C");
new Node<string> node2("B");
new Node<string> node3("C");
headPtr = headPtr->getNext();


Any improvements?
The headPtr is initialized to contain nullptr.

Who says so?

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.
Last edited on
Topic archived. No new replies allowed.