Your Node
implementation is atypical of a common implementation. A node is really just a part of the linked list and just needs two data members: its data and a pointer to the next node (and a pointer to the previous node for a doubly-linked list). It does not need its own member functions, but it could also have its own constructor, instead of having the list itself take care of it.
template <typename T>
struct Node
{
T data;
Node* next;
Node(T data) : data(data), next(nullptr) {}
};
Although this can be a class
, it's okay to make it a struct
because it can just be defined inside of the LinkedList
class, safely as a private
data member.
Misc.:
You should remove
<iostream>
since it's unused, unless you're going to implement a display function inLinkedList
. If you overloadoperator<<
instead, then you'll just need<ostream>
. Regardless, this doesn't belong toNode
.getCount()
should beconst
as it's not supposed to modify any data members:int getCount() const { return _count; }