#include #include template class ListNode { public: ListNode(T* init_elem, ListNode* init_next) : element(init_elem), next(init_next) {} T * element; // Stores items externally ListNode * next; // Note recursive template use }; template class LinkedList { public: LinkedList() { head = NULL; } void append(T* value); T* removeFront(); private: ListNode * head; // Note use of previous template }; template void LinkedList::append(T* value) { if (head == NULL) { head = new ListNode(value, NULL); } else { ListNode * cursor = head; while (cursor->next != NULL) cursor = cursor->next; cursor->next = new ListNode(value, NULL); } } template T* LinkedList::removeFront() { assert(head != NULL); ListNode* old_head = head; T* old_element = old_head->element; head = head->next; delete old_head; return old_element; } int main() { LinkedList ilist; int x = 1, y = 2, z = 3; ilist.append( &x ); cout << *ilist.removeFront() << endl; ilist.append( &y ); cout << *ilist.removeFront() << endl; ilist.append( &z ); cout << *ilist.removeFront() << endl; return 0; }