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++)
       B.erase(0);

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



   // 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
       B.insert(n,*a);
       a++;

      if (a == A.end()) break;
        // now give C an element
        C.insert(n,*a);
        a++;
        n=n+1;


      }



}

So it is the same as the function above but the chain that made A & B is empty at the end?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.