I am trying to understand the difference or why I can create a Linked List using a class constructor vs creating it with a struct(when inserting a node from the head of the list)?
Ex:
//head = NULL;
void addNode(theData) {
head = new ClassName(theData, head);
}
//This data would get being inserted at the head of the linked list, while creating a link between nodes. Also the first insert will have NULL in it, so I can use that to flow threw the linked list.
But if I have a struct
And I go
void addNode(theData) {
head = new ClassName;
head->data=theData;
}
There is no place to put in the the "head", like in the constructor if I was using a class? And if I do this
void addNode(theData) {
head = new ClassName;
head->data=theData;
head ->next = NULL;
}
Then it is not continuously adding to the list from the head, it is only adding one Node and recopying over the node over and over again.
So it is my understanding that the "new operator" creates a new node and head points to that. So I understand why the constructor why works for inserting into the head of the list. By putting the "head" in the constructor with the "new" operating you are creating a whole new node and re pointing the pointer at it while also creating a link between the nodes.
I don't see how that is possible using Struct when trying to insert from the head of the list.
Am I looking at this correctly? Am I understanding the terminology right?
Ex:
//head = NULL;
void addNode(theData) {
head = new ClassName(theData, head);
}
//This data would get being inserted at the head of the linked list, while creating a link between nodes. Also the first insert will have NULL in it, so I can use that to flow threw the linked list.
But if I have a struct
And I go
void addNode(theData) {
head = new ClassName;
head->data=theData;
}
There is no place to put in the the "head", like in the constructor if I was using a class? And if I do this
void addNode(theData) {
head = new ClassName;
head->data=theData;
head ->next = NULL;
}
Then it is not continuously adding to the list from the head, it is only adding one Node and recopying over the node over and over again.
So it is my understanding that the "new operator" creates a new node and head points to that. So I understand why the constructor why works for inserting into the head of the list. By putting the "head" in the constructor with the "new" operating you are creating a whole new node and re pointing the pointer at it while also creating a link between the nodes.
I don't see how that is possible using Struct when trying to insert from the head of the list.
Am I looking at this correctly? Am I understanding the terminology right?