0

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;


      }



}

Edited by Dani: Formatting fixed

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by ashsha
0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.