1 //tstList.cpp -- by tsaiwn@csie.nctu.edu.tw 2 #include "list" 3 //use it if there is a file named "list" in current directory 4 //otherwise will use the C++ STL include file 5 #include 6 using namespace std; 7 template void print(list &a) { // print list 8 typename list::iterator it; // do NOT forget "typename" 9 for( it=a.begin( ); it!= a.end( ); ++it) cout << " "<< *it; 10 cout << endl; 11 } 12 template void prHeadTail(list a) { 13 typename list::iterator hh = a.begin( ); // front 14 typename list::iterator tail = a.end( ); // behind last 15 --tail; // move back to point to the last element 16 //typename is required in template to tell compiler that ... 17 cout << " size = " << a.size( )<< ", "; 18 // Note that there is no capacity( ) for list 19 cout << " Head=" << *hh<<", " << " Tail=" << *tail; 20 cout << "; front=" << a.front( )<<", back="< gg; // should use list in Java 24 list yy; // should use list in Java 25 cout << "Initial gg: "; prHeadTail(gg); 26 cout << "Initial yy: "; prHeadTail(yy); 27 gg.push_back(123); 28 gg.push_back(456); gg.push_back(38); 29 gg.push_front(888); gg.push_front(58); 30 cout << "Original list gg: "; // gg.front( ) = 168; 31 print(gg); 32 prHeadTail(gg); 33 gg.sort( ); 34 cout << "After sort, gg: "; print(gg); prHeadTail(gg); 35 yy.push_back(12.5); yy.push_back(3.8); yy.push_back(4.9); 36 cout << "after yy.push_back( ) yy: "; 37 print(yy); prHeadTail(yy); 38 return 0; 39 }// main( // see http://www.cplusplus.com 40 /****** First, test my "list" <======== in current directory 41 C:\testc\STL>dir list 42 2010/05/29 ¤U¤È 11:58 2,600 list 43 ... 44 C:\testc\STL>g++ tstList.cpp 45 C:\testc\STL>a 46 Initial gg: size = 0, Head=0, Tail=0; front=0, back=0 47 Initial yy: size = 0, Head=0, Tail=0; front=0, back=0 48 Original list gg: 58 888 123 456 38 49 size = 5, Head=58, Tail=38; front=58, back=38 50 After sort, gg: 58 888 123 456 38 51 size = 5, Head=58, Tail=38; front=58, back=38 52 after yy.push_back( ) yy: 12.5 3.8 4.9 53 size = 3, Head=12.5, Tail=4.9; front=12.5, back=4.9 54 55 //////=== now force to use C++ STL because rename list 56 C:\testc\STL>ren list listME 57 C:\testc\STL>g++ tstList.cpp 58 C:\testc\STL>a 59 Initial gg: size = 0, Head=8, Tail=8; front=8, back=8 60 Initial yy: size = 0, Head=5.28441e-308, Tail=5.28441e-308; 61 front=5.28441e-308, back=5.28441e-308 62 Original list gg: 58 888 123 456 38 63 size = 5, Head=58, Tail=38; front=58, back=38 64 After sort, gg: 38 58 123 456 888 65 size = 5, Head=38, Tail=888; front=38, back=888 66 after yy.push_back( ) yy: 12.5 3.8 4.9 67 size = 3, Head=12.5, Tail=4.9; front=12.5, back=4.9 68 ********************************************/