CS210 Lab: Templates--Generic Linked List


Overview:

The following is the class definition for a linked list of integers. This will be placed in a ".h" file. The member functions will be defined in a separate ".cpp" file (not included here):
Class Class Template
 
 class LinkedList;

 
 class ListElement
 {
       int datum;
       ListElement* next;
       ListElement (int const&, ListElement*);

       friend LinkedList;
 };

 
 class LinkedList
 {
       ListElement* head;
 public:
       LinkedList ();
       ~LinkedList ();
       bool IsEmpty () const;
       int const& First () const;
       int const& Last () const;
       void Prepend (int const&);
       void Append (int const&);
       void Extract (int const&);
       void Print ();
 };
 template <class T>
 class LinkedList;

 template <class T>
 class ListElement
 {
       T datum;
       ListElement* next;
       ListElement (T const&, ListElement*);

       friend LinkedList <T>;
 };

 template <class T>
 class LinkedList
 {
       ListElement* head;
 public:
       LinkedList ();
       ~LinkedList ();
       bool IsEmpty () const;
       T const& First () const;
       T const& Last () const;
       void Prepend (T const&);
       void Append (T const&);
       void Extract (T const&);
       void Print ();
 };

Back to Templates Lab click here

CS Dept Home Page
CS Dept Class Files
CS210 Class Files

Copyright: Department of Computer Science, University of Regina.