[13.4] Implementing Container Types with Templates

Here is the original C++ source file and the C++ Glossary.
Here is the previous example program (swap2.cc), and here is a sample client routine (lmain.cc).
001:                                 // Chapter 13 - Program 3
002: #ifndef LIST_H
003: #define LIST_H
004: template <class T>
005: class Node {
006:   public:
007:     T n_data;
008:     Node<T> *n_next;
009:     Node<T> *n_prev;
010: 
011:     Node(const T &data, Node<T> *next = 0, Node<T> *prev = 0) {
012:         n_data = data;
013:         n_next = next;
014:         n_prev = prev;
015:     }
016: };
017: 
018: template <class T>
019: class List {
020:     Node<T> *l_headp;
021:     Node<T> *l_tailp;
022:     Node<T> *l_currp;
023:     List<T>& copy(const List<T>& other) {
024:         for (Node<T> *walkp = other.l_headp; walkp; walkp = walkp->n_next)
025:           append(walkp->n_data);
026:         return(*this);
027:     }
028: 
029:   public:
030:     List() { l_headp = l_tailp = l_currp = 0; }
031:     List(const List<T>& other)
032:       { l_headp = l_tailp = l_currp = 0; copy(other); }
033:     List<T>& operator=(const List<T>& other) { flush(); copy(other); }
034:     ~List() { flush(); }
035:     int empty() const { return !l_headp; }
036:     void append(const T &elem) {
037:         if (!l_headp) {
038:             l_headp = l_tailp = l_currp = new Node<T>(elem);
039:         } else {
040:             l_tailp->n_next = new Node<T>(elem, 0, l_tailp);
041:             l_tailp = l_tailp->n_next;
042:         }
043:     }
044:     void flush() {
045:         Node<T> *walkp = l_headp;
046:         while (walkp) {
047:             Node<T> *nextp = walkp->n_next;
048:             delete walkp;
049:             walkp = nextp;
050:         }
051:         l_headp = l_tailp = l_currp = 0;
052:     }
053:     void rewind() { l_currp = l_headp; }
054:     T &current() const { return l_currp->n_data; }
055:     void advance() { l_currp = l_currp->n_next; }
056:     int hascur() const { return (l_currp != 0); }
057: };
058: #endif /* LIST_H */