From A C++ beginner. I have written a class that splits the first list into 2.I used iterators to accomplish it. Now I am supposed to create a new method that splits the input chain ∗this, destroys the input chain and uses its nodes to construct the chains a and b. I am lost.

template <class T>
void chain<T>::Split(chain<T>& A, chain<T>& B, chain<T>& C)

{// Split A into two chains B and C.

 // When done, A is unchanged.

   // first free all nodes in B and C

    int j;
    j=0 ;
    listSize = B.listSize;
   for (j=0 ; j < listSize; j++)

  // C.Erase();
   j=0 ;
    listSize = C.listSize;
   for (j=0 ; j < listSize; j++)

   // assign elements alternately to B and C
   int n=0;
   chain<T>::iterator a = A.begin(); // find first node of A
   while (a != A.end()) {
    // first give B an element

      if (a == A.end()) break;
        // now give C an element



I need to create a new split that deletes chain A first (*this) and then use the nodes to create B and C. The above code leaves A unchanged. Someone suggested to save the first node somewhere, then delete the "chain" object, and go on from there. I know how to save the first node. and I think to delete the chain object, I should use: delete A; I am lost as what to do after that. it seems that if I destroy *this, I could not use its nodes.

This article has been dead for over six months. Start a new discussion instead.