Back to July Calendar 

Previous Entry Next Entry→

September 17, 2005

Looking at the possibility of enrolling (or paying tuition) in CVN course at  Very expensive, and would require approval from Sabbatical committee and there's very little time in the maybe next semester?

How about some Deitel Chapter 17 exercises? 

17.6: Write a program that concatenates two linked list objects of characters.  The program should include function concatenate, which takes references to both list objects as arguments

For this, you don't need to write a member function or expand the list class in any way.  Here's the concatenate function:

// concatenates second List to first List
// pass addresses of each abstract data type

template<class T>
void concatenate( List<T> &first, List< T > &second ) {
   List<T> temp(second); // use copy constructor to create a copy of second
   // value will keep the item removed from the front of temp
   T value;
   while ( !temp.isEmpty() ) {
      // remove value from the front of the temp list...
      temp.removeFromFront( value );
      // ...and insert it at end of first list
      first.insertAtBack( value );
   } // end while
} // end function concatenate

And here's an implementation of it:

int main() {
   List<char> firstList;
   List<char> secondList;
   char c;
   cin >> c;
   while (c != 'q') {
      firstList.insertAtBack( c );
      cin >> c;

   cin >> c;
   while (c != 'q') {
      secondList.insertAtBack( c );
      cin >> c;


   concatenate( firstList, secondList );
   cout << "The new firstList after concatenation is:\n";
   char blueb;
   cin >> blueb;
   return 0;
} // end main


The list is: c a t

The list is: d o g

All nodes destroyed

The new firstList after concatenation is:
The list is: c a t d o g