Listing 2 is the second trial. This time, we use a class template to parametrize the type of the payload of a list node.
Listing 2: | Templateclasslinkedlist |
Here, we use T as a “parameter” so that we can create different template classes by using a different actual type for T. For example, to specify a linked list of characters, we can use LinkedList<char>, and to specify a linked list of long integers, we can use LinkedList<long int>.
Note that T is not limited only to built-in types. If we have a class called Fraction, we can have a linked list of fractions as LinkedList<Fraction>.