Part of my homework assignment is to implement a generic linked list. So far I wrote this function:
template<class T>
void List<T>::insert(const T& data)
{
List<T>::Node* newNode = new List<T>::Node(data);
newNode->next = nullptr;
if (head == nullptr)
{
head = newNode;
tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
size++;
}
As you can see I'm getting the data by reference but I could get it by value as well. My question is what approach is better and why?