Script started on Tue Mar 27 03:40:29 2007 [tsaiwn@ccbsd3]queue> [tsaiwn@ccbsd3]queue> cat -n myq5.h 1 //myq5.h -- inplement a queue 2 #include 3 using namespace std; 4 template 5 class MyStack { 6 list x; 7 public: 8 void push(const T& y) { 9 x.push_back(y); // tail 10 } 11 T top( ) { 12 return x.front( ); // head 13 } 14 void pop( ) { 15 x.pop_front( ); // drop head 16 } 17 bool empty( ) { return x.begin() == x.end(); } 18 }; [tsaiwn@ccbsd3]queue> cat -n testmyq5.cpp 1 // testmyq5.cpp -- by tsaiwn 2 // g++ testmyq5.cpp ; ./a.out 3 using namespace std; 4 #include "myq5.h" 5 //注意以下是 1999之後的 C++ 新寫法, 舊法使用 6 #include 7 int main( ){ 8 MyStack x; // 注意這! 其實是 queue 9 x.push(880); 10 x.push(770); 11 x.push(53); 12 while(!x.empty( )){ 13 cout << x.top( ) << " "; x.pop( ); 14 } 15 cout << endl; 16 } [tsaiwn@ccbsd3]queue> g++ testmyq5.cpp [tsaiwn@ccbsd3]queue> ./a.out 880 770 53 [tsaiwn@ccbsd3]queue> exit exit Script done on Tue Mar 27 03:40:55 2007