Listing 9 Implementation using static data member

//
// list.cpp - list implementation using a nested class
// with a static data member for counting the objects
//

#include <stdio.h>

#include "list.h"

unsigned list::count = 0;

list::list(unsigned n)
       {
       first = last = new node (n, 0);
       ++count;
       }

list::~list()
       {
       node *p;
       while ((p = first) != 0)
              {
              first = first->next;
              delete p;
              }
       --count;
       }

void list::add(unsigned n)
       {
       if (last->number != n)
              last = last->next = new node (n, 0);
       }

void list::print()
       {
       for (node *p = first; p != 0; p = p->next)
             printf("%4u ", p->number);
       }

// End of File