Here is a list with iterators, both a single link list and a double link list ... each with both merge sort and insert sort:
First the test program ...
// List2IterNode_h_test.cpp // this version 2010-10-15 //
#include <fstream>
//#include <string>
#include <ctime> // re. clock ...
// defaults to testing file "List1IterNode.h"
#define link2 false // change 'false' to 'true' to test file "List2IterNode.h"
#define do_part1 true // change 'true' to 'false' to skip file test in 'part1'
#if link2
#define links "double-link linked list"
#include "List2IterNode.h"
// includes <iostream>, <string>, <cstdlib>, using namespace std; and myAssert
#else
#define links "single-link linked list"
#define insert_before insert
#include "List1IterNode.h"
// includes <iostream>, <string>, <cstdlib>, using namespace std; and myAssert
#endif
// above ListxIterNode.h includes ...
/*
#include <iostream>
#include <string>
#include <cstdlib> // re. exit ...
using namespace std;
*/
const char DATAFILE[] = "randInt3M.txt";
bool fillFromFile( List< int >& il )
{
ifstream fin( DATAFILE );
if( !fin ) return false;
int tmp;
while( fin >> tmp ) il.push_back( tmp );
fin.close();
return true;
}
#if link2
bool writeFile( const List< int >& il )
{
ofstream fout( "randInt3M_rev_sorted.txt" );
if( !fout ) return false;
for( Iter< int > pos = il.end(); pos != il.begin(); )
fout << *--pos << "\n";
return true;
}
#else
bool writeFile( const List< int >& il )
{
ofstream fout( "randInt3M_sorted.txt" );
if( !fout ) return false;
for( Iter< int > pos = il.begin(), end = il.end(); pos != end; ++pos )
fout << *pos << "\n";
return true;
}
#endif
bool createFile( List< int >& il )
{
ofstream fout( DATAFILE );
if( !fout ) return false;
srand( (unsigned) time(0) );
for( int i = 0; i < 3000000; ++i )
{
int tmp = rand();
il.push_back( tmp );
fout << tmp << " ";
if( i % 10 == 0 ) fout << endl;
}
fout.close();
return true;
}
int main()
{
cout << "For " << links << " ...\n\n";
cout << "sizeof(List<int>) = " << sizeof(List<int>)
<< ", sizeof(Iter<int>) = " << sizeof(Iter<int>)
<< ", sizeof(Node<int>) = " << sizeof(Node<int>) << " ...\n\n";
#if do_part1
List< int > ml; // construct an empty list to hold int's ...(here 3 Million)
cout << "time in sec's to execute fillFromFile( ml ) = " << flush;
double ti = clock();
if( !fillFromFile( ml ))
{
if( createFile( ml )) cout << "\n(created) ";
else
{
cerr << "\nError in creating data file!"
<< "\nPress 'Enter' to continue/exit ... " << flush;
cin.get();
return 1;
}
}
double tp = clock() - ti;
cout << tp/CLOCKS_PER_SEC << " and ml.get_size() = " << ml.get_size();
cout << "\ntime in sec's to execute ml.msort() = " << flush;
ti = clock();
ml.msort();
tp = clock() - ti;
cout << tp/CLOCKS_PER_SEC << " and ml.isSorted() = "<< ml.isSorted() << endl;
cout << "time in sec's (pre-sorted) to execute ml.msort() = " << flush;
ti = clock();
ml.msort();
tp = clock() - ti;
cout << tp/CLOCKS_PER_SEC << " and ml.isSorted() = "<< ml.isSorted() << endl;
List< int > mlRev;
for( Iter< int > it = ml.begin(), end = ml.end(); it != end; ++it )
mlRev.push_front( *it );
cout << "time in sec's (rev-sorted) to execute ml.msort() = "
<< flush;
ti = clock();
mlRev.msort();
tp = clock() - ti;
cout << tp/CLOCKS_PER_SEC << " and ml.isSorted() = "<< mlRev.isSorted() << endl;
int count = 0;
#if link2
// for double-link linked list ...
for( Iter< int > it = ml.begin(), end = ml.end(); it != end; )
{
if( *it == 32767 ) // try also removing all 0's ...
{
++count;
it = ml.erase( it );
}
else ++it;
}
#else
// for single-link linked list ...
for( Iter< int > it = ml.begin(), end = ml.end(), prev = end ; it != end ; )
{
if( *it == 32767 ) // try also removing all 0's ...
{
++count;
it = ml.erase( it, prev ); // for single linked-list ...
}
else
{
prev = it;
++it;
}
}
#endif // link2
cout << "\nThe count of 32767's = " << count << endl;
cout << "\nThe new size is ml.get_size() = " << ml.get_size() << endl;
ml.remove_duplicates();
cout << "\nThe new size is ml.get_size() = " << ml.get_size() << endl;
count = 0;
Iter< int > it = ml.begin(), end = ml.end();
while( it != end )
{
if( *it == 32767 ) ++count;
++it;
}
cout << "\nThe count NOW of 32767's = " << count << endl;
cout << "time in sec's, for REDUCED list, to execute writeFile( ml ) = "
<< flush;
ti = clock();
writeFile( ml ); // test of reverse iter & previous links after msort was ok
tp = clock() - ti;
cout << tp/CLOCKS_PER_SEC << "\nand ml.get_size() = "<< ml.get_size() << endl;
cout << "\nWait briefly ... freeing large list now ..." << flush;
ml.free();
cout << "\nPress 'Enter' to continue ... " << flush;
cin.get();
#endif // do_part1
List< string > students; // construct an empty List (of strings) ...
cout << "\nstudents.get_size() = " << students.get_size() << "\n";
students.push_back( "Cane, Sugar" );
students.push_back( "Dame, Lady" );
students.push_back( "Fame, Fanny" );
students.push_back( "Game, Pretty" );
cout << "students.get_size() = " << students.get_size() << "\n";
// add a value in 5th position ...
Iter< string > pos, poscpy;
pos = students.begin();
++pos;
++pos;
++pos;
++pos;
students.insert( pos, "Tame, Very" );
cout << "students.get_size() = " << students.get_size() << "\n";
students.insert( pos, "Tame, Very Very" );
//print the list ...
for( pos = students.begin(); pos != students.end(); ++pos )
cout << *pos << "\n";
cout << "students.get_size() = " << students.get_size() << "\n";
#if link2
// remove the 3rd last value ...
pos = students.end();
--pos;
--pos;
--pos;
#else
// remove the 4th value ...
pos = students.begin();
++pos;
++pos;
++pos;
#endif
poscpy = students.erase(pos);
//print the list ...
cout << "\nAter removing 3rd last value ...\n";
for( pos = students.begin(); pos != students.end(); ++pos )
cout << *pos << "\n";
cout << "students.get_size() = " << students.get_size() << "\n";
// insert a student at 'poscpy' returned above ...
students.insert( poscpy, "Anthony, Timothy Horton" );
//print the list ...
cout << "\nAfter insertion ...\n";
for( pos = students.begin(); pos != students.end(); ++pos )
cout << *pos << "\n";
cout << "students.get_size() = " << students.get_size() << "\n";
#if link2
//print the list in reverse order ...
cout << "\nPrinting list in reverse order ...\n";
for( pos = students.end(); pos != students.begin(); )
cout << *--pos << "\n";
cout << "students.get_size() = " << students.get_size() << "\n";
#else
//print the list
cout << "\nPrinting list ...\n";
for( pos = students.begin(); pos != students.end(); ++pos )
cout << *pos << "\n";
cout << "students.get_size() = " << students.get_size() << "\n";
#endif
List < string > s_copy( students );
students.free();
cout << "\nAfter students.free() ... ";
cout << "students.get_size() = " << students.get_size() << "\n";
cout << "\nPrinting s_copy ...\n";
for( pos = s_copy.begin(); pos != s_copy.end(); ++pos )
cout << *pos << "\n";
cout << "s_copy.get_size() = " << s_copy.get_size() << "\n";
s_copy.push_front( "Saucer, Lucy" );
if( !s_copy.isSorted() ) { cout << "\nsorting ..."; s_copy.isort(); }
#if link2
cout << "\nPrinting s_copy rev...\n";
for( pos = s_copy.end(); pos != s_copy.begin(); )
cout << *--pos << "\n";
#else
cout << "\nPrinting s_copy ...\n";
for( pos = s_copy.begin(); pos != s_copy.end(); ++pos )
cout << *pos << "\n";
#endif
cout << "s_copy.get_size() = " << s_copy.get_size() << "\n";
s_copy.isort_insert( "Georgette, Baby" );
s_copy.update_pRear(); // update links ...
#if link2
cout << "\nAfter isort_insert(...) ... printing s_copy now rev...\n";
for( pos = s_copy.end(); pos != s_copy.begin(); )
cout << *--pos << "\n";
#else
cout << "\nAfter isort_insert(...) ... printing s_copy now ...\n";
for( pos = s_copy.begin(); pos != s_copy.end(); ++pos )
cout << *pos << "\n";
List< string > rev;
for( pos = s_copy.begin(); pos != s_copy.end(); ++pos )
rev.push_front( *pos );
cout << "\nAfter making a rev copy ... printing rev now ...\n";
for( pos = rev.begin(); pos != rev.end(); ++pos )
cout << *pos << "\n";
cout << "rev.get_size() = " << rev.get_size() << "\n";
#endif
cout << "s_copy.get_size() = " << s_copy.get_size() << "\n";
pos = s_copy.find( "Anthony, Timothy Horto" ); // test can't find ...
s_copy.insert( pos, "Lace, Fanny" );
cout << "\nAfter insert ... printing s_copy ...\n";
for( pos = s_copy.begin(); pos != s_copy.end(); ++pos )
cout << *pos << endl;
if( (pos = s_copy.find( "Anthony, Timothy Horton" )) != s_copy.end() )
s_copy.erase( pos );
cout << "\nAfter erase ... printing s_copy ...\n";
for( pos = s_copy.begin(); pos != s_copy.end(); ++pos )
cout << *pos << endl;
cout << "\nPress 'Enter' to continue ... " << flush;
cin.get();
}
// example output ...
/*
For single-link linked list ...
sizeof(List<int>) = 16, sizeof(Iter<int>) = 4, sizeof(Node<int>) = 8 ...
time in sec's to execute fillFromFile( ml ) = 3.828 and ml.get_size() = 3000000
time in sec's to execute ml.msort() = 6.875 and ml.isSorted() = 1
time in sec's (pre-sorted) to execute ml.msort() = 8.688 and ml.isSorted() = 1
time in sec's (rev-sorted) to execute ml.msort() = 0.875 and ml.isSorted() = 1
The count of 32767's = 92
The new size is ml.get_size() = 2999908
The new size is ml.get_size() = 32767
The count NOW of 32767's = 0
time in sec's, for REDUCED list, to execute writeFile( ml ) = 0.078
and ml.get_size() = 32767
Wait briefly ... freeing large list now ...
Press 'Enter' to continue ...
students.get_size() = 0
students.get_size() = 4
students.get_size() = 5
Cane, Sugar
Dame, Lady
Fame, Fanny
Game, Pretty
Tame, Very
Tame, Very Very
students.get_size() = 6
Ater removing 3rd last value ...
Cane, Sugar
Dame, Lady
Fame, Fanny
Tame, Very
Tame, Very Very
students.get_size() = 5
After insertion ...
Cane, Sugar
Dame, Lady
Fame, Fanny
Anthony, Timothy Horton
Tame, Very
Tame, Very Very
students.get_size() = 6
Printing list ...
Cane, Sugar
Dame, Lady
Fame, Fanny
Anthony, Timothy Horton
Tame, Very
Tame, Very Very
students.get_size() = 6
After students.free() ... students.get_size() = 0
Printing s_copy ...
Cane, Sugar
Dame, Lady
Fame, Fanny
Anthony, Timothy Horton
Tame, Very
Tame, Very Very
s_copy.get_size() = 6
sorting ...
Printing s_copy ...
Anthony, Timothy Horton
Cane, Sugar
Dame, Lady
Fame, Fanny
Saucer, Lucy
Tame, Very
Tame, Very Very
s_copy.get_size() = 7
After isort_insert(...) ... printing s_copy now ...
Anthony, Timothy Horton
Cane, Sugar
Dame, Lady
Fame, Fanny
Georgette, Baby
Saucer, Lucy
Tame, Very
Tame, Very Very
After making a rev copy ... printing rev now ...
Tame, Very Very
Tame, Very
Saucer, Lucy
Georgette, Baby
Fame, Fanny
Dame, Lady
Cane, Sugar
Anthony, Timothy Horton
rev.get_size() = 8
s_copy.get_size() = 8
After insert ... printing s_copy ...
Anthony, Timothy Horton
Cane, Sugar
Dame, Lady
Fame, Fanny
Georgette, Baby
Saucer, Lucy
Tame, Very
Tame, Very Very
Lace, Fanny
After erase ... printing s_copy ...
Cane, Sugar
Dame, Lady
Fame, Fanny
Georgette, Baby
Saucer, Lucy
Tame, Very
Tame, Very Very
Lace, Fanny
Press 'Enter' to continue ...
////////////////////////////////////////////////////////////////////////////////
For double-link linked list ...
sizeof(List<int>) = 16, sizeof(Iter<int>) = 8, sizeof(Node<int>) = 12 ...
time in sec's to execute fillFromFile( ml ) = 5.343 and ml.get_size() = 3000000
time in sec's to execute ml.msort() = 7.282 and ml.isSorted() = 1
time in sec's (pre-sorted) to execute ml.msort() = 9.125 and ml.isSorted() = 1
time in sec's (rev-sorted) to execute ml.msort() = 1.172 and ml.isSorted() = 1
The count of 32767's = 92
The new size is ml.get_size() = 2999908
The new size is ml.get_size() = 32767
The count NOW of 32767's = 0
time in sec's, for REDUCED list, to execute writeFile( ml ) = 0.063
and ml.get_size() = 32767
Wait briefly ... freeing large list now ...
Press 'Enter' to continue ...
students.get_size() = 0
students.get_size() = 4
students.get_size() = 5
Cane, Sugar
Dame, Lady
Fame, Fanny
Game, Pretty
Tame, Very
Tame, Very Very
students.get_size() = 6
Ater removing 3rd last value ...
Cane, Sugar
Dame, Lady
Fame, Fanny
Tame, Very
Tame, Very Very
students.get_size() = 5
After insertion ...
Cane, Sugar
Dame, Lady
Fame, Fanny
Anthony, Timothy Horton
Tame, Very
Tame, Very Very
students.get_size() = 6
Printing list in reverse order ...
Tame, Very Very
Tame, Very
Anthony, Timothy Horton
Fame, Fanny
Dame, Lady
Cane, Sugar
students.get_size() = 6
After students.free() ... students.get_size() = 0
Printing s_copy ...
Cane, Sugar
Dame, Lady
Fame, Fanny
Anthony, Timothy Horton
Tame, Very
Tame, Very Very
s_copy.get_size() = 6
sorting ...
Printing s_copy rev...
Tame, Very Very
Tame, Very
Saucer, Lucy
Fame, Fanny
Dame, Lady
Cane, Sugar
Anthony, Timothy Horton
s_copy.get_size() = 7
After isort_insert(...) ... printing s_copy now rev...
Tame, Very Very
Tame, Very
Saucer, Lucy
Georgette, Baby
Fame, Fanny
Dame, Lady
Cane, Sugar
Anthony, Timothy Horton
s_copy.get_size() = 8
After insert ... printing s_copy ...
Anthony, Timothy Horton
Cane, Sugar
Dame, Lady
Fame, Fanny
Georgette, Baby
Saucer, Lucy
Tame, Very
Tame, Very Very
Lace, Fanny
After erase ... printing s_copy ...
Cane, Sugar
Dame, Lady
Fame, Fanny
Georgette, Baby
Saucer, Lucy
Tame, Very
Tame, Very Very
Lace, Fanny
Press 'Enter' to continue ...
*/