* deanfamily:
Here's the problem:
I have two linked lists of integers (list1 and list2) that are already in
sequential order, and I need to combine them into one list in sequential
order (newList). Any thoughts?
#include <algorithm> // std::copy
#include <iterator> // std:
stream_iterator
#include <iostream> // std::cout
#include <ostream> // <<, std::endl
#include <memory> // std::auto_ptr
#include <list> // std::list
typedef std::list<int> IntList;
typedef std::auto_ptr<IntList> IntListAutoPtr;
void display( IntList const& list )
{
std:
stream_iterator<int> out( std::cout, " " );
std::copy( list.begin(), list.end(), out );
std::cout << std::endl;
}
void moveToTailOf( IntList& left, IntList& right )
{
left.splice( left.end(), right );
}
IntListAutoPtr destructiveConcat( IntList& left, IntList& right )
{
IntListAutoPtr result( new IntList );
moveToTailOf( *result, left );
moveToTailOf( *result, right );
return result;
}
IntListAutoPtr concat( IntList const& left, IntList const& right )
{
IntList copyOfLeft( left );
IntList copyOfRight( right );
return destructiveConcat( copyOfLeft, copyOfRight );
}
int main()
{
IntList a, b;
for( int x = 1; x <= 3; ++x ) { a.push_back( x ); }
for( int x = 101; x <= 103; ++x ) { b.push_back( x ); }
display( a );
display( b );
display( *concat( a, b ) );
}