In a linked list class, the code below is declaring Node<T> *newNode
:
void LinkedList<T>::push_front(const T& val) {
Node<T> *newNode = new Node<T>(val);
newNode->next = head;
head = newNode;
if(newNode->next == NULL) tail = newNode;
}
Why do we use dynamic allocation? Can't we just write Node<T> newNode(val);
?
I think since we are not making arrays of newNode
, it is valid.