2.7. Copying and Clean-up

BidderCollection::BidderCollection (const BidderCollection& bc)
  : size(bc.size)
{
  list.append(bc.list);
}


BidderCollection& BidderCollection::operator= (const BidderCollection& bc)
{
  if (this != &bc)
    {
      list.clear();
      size = bc.size;
      list.append(bc.list);
    }
  return *this;
}

append

// Add all values from another list onto the end of this one
template <typename Data>
void LListHeader<Data>::append (const LListHeader<Data>& list)
{
  // Move to last node 
  LListNode<Data>* last = first;
  while (last->next != NULL)
    last = last->next;

  // Append new nodes onto end of list
  const LListNode<Data>* current = list.first;
  while (current != NULL)
    {
      LListNode<Data>* newNode = new LListNode<Data>(current->data, NULL);
      if (last != NULL)
          last->next = newNode;
      last = newNode;
    }
}

A traversal to the end of the current list, followed by repeated "addToEnd" equivalents.

Collection Destructor

BidderCollection::~BidderCollection ()
{
  list.clear();
}

clear

template <typename Data>
void LListHeader<Data>::clear()
{
  LListNode<Data>* current = first;
  LListNode<Data>* nxt = NULL;
  while (current != NULL)
    {
      nxt = current->next;
      delete current;
      current = nxt;
    }
  first = NULL;
}

Only "trick" here is that we cant do the usual

current = current->next;

at the end of the loop, because we will have already deleted the node that contains next.

The List-based Collection