CHAPTER3-16.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:4k
源码类别:

STL

开发平台:

C/C++

  1. //文件名:CHAPTER3-16.cpp
  2. // Template class for storing list elements
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. template <class T>                          // Use template keyword
  7. class ListElement //定义类ListElement,用于表示list对象
  8. {
  9. public:
  10.     T data;
  11.     ListElement<T> * next ;
  12.     ListElement(T& i_d, ListElement <T>* i_n)
  13.     : data(i_d),next(i_n) { }
  14.     ListElement<T>* copy()               // copy includes all next elements
  15.     { return new ListElement(data,(next?next->copy():0)); }
  16. };
  17. template <class T> class ListIterator //定义类ListIterator,用于访问和操作list对象
  18. { public :
  19.    //ListIterator(List<T>& l) ;
  20.    T operator()() ;
  21.    int operator++() ;
  22.    int operator!() ;
  23.  public:
  24.  ListElement<T>* rep ;
  25. };
  26. template <class T>T ListIterator<T>::operator() ()
  27. { if (rep) return rep->data;
  28.   else 
  29.   {    
  30.   T tmp ;    return tmp ;           // Default value  
  31.   }
  32. }
  33. template <class T>int ListIterator<T>::operator++(){   if (rep)     rep = rep->next ;   return (rep != 0) ;}
  34. template <class T>int ListIterator<T>::operator!(){   return (rep != 0) ;}
  35. template <class T>class List //定义类List 
  36. {public:
  37.     friend class ListIterator<T> ;
  38.     List();
  39.     List(const List&);
  40. ~List();
  41.     List<T>& operator=(const List<T>&);
  42. // typical list ops
  43. T head();
  44.     List<T> tail();
  45.     void add(T&);
  46.     friend ostream& operator<<(ostream&, const List<T>&);
  47. public:
  48.     void clear() ; // Delete all list elements
  49.     ListElement<T>* rep ;
  50. };
  51. // Default Constructor
  52. template <class T>
  53. List<T>::List(){  rep = 0 ; }
  54. // Copy Constructor
  55. template <class T>List<T>::List(const List<T>& l){     rep = l.rep ? l.rep->copy() : 0 ;}
  56. // Overloaded assignment operator
  57. template <class T>List<T>& List<T>::operator=(const List<T>& l)
  58. {  if (rep != l.rep)   {        clear() ;        rep = l.rep ? l.rep->copy() : 0 ;    }
  59.     return *this ;
  60. }
  61. // Destructor
  62. template <class T>List<T>::~List(){  clear() ;}
  63. // Delete representation
  64. template <class T>
  65. void List<T>::clear()
  66. {   while (rep)
  67.     {   ListElement<T>* tmp = rep ;
  68.         rep = rep->next ;
  69.         delete tmp ;
  70.     }
  71.     rep = 0 ;
  72. }
  73. // Add element to front of list
  74. template <class T>void List<T>::add(T& i){   rep = new ListElement<T>(i,rep) ;}
  75. // Return head of list or default value of type T
  76. template <class T>T List<T>::head(){ if (rep) return rep->data ; else {T tmp ; return tmp ;}}
  77. // Return tail of list or empty list
  78. template <class T>List<T> List<T>::tail()
  79. {    List<T> tmp ;
  80.      if (rep)
  81.        if (rep->next) tmp.rep = rep->next->copy() ;
  82.      return tmp ;
  83. }
  84. // Output operator
  85. template <class T>ostream& operator<<(ostream& os, const List<T>& l)
  86. {  if (l.rep)
  87.    {  ListElement<T>* p = l.rep ;
  88.       os << "( " ;
  89.       while (p){ os << p->data << " " ;   p = p->next ; }
  90.       os << ")n" ;
  91.   }
  92.   else
  93.   os << "Empty listn" ;
  94.   return os ;
  95. }
  96. int main()
  97. {
  98.    List<int> l ;            // Integer list
  99.    cout << l ;
  100.    int i=1;
  101.    l.add(i) ;
  102.    i=2;
  103.    l.add(i) ;
  104.    i=3;
  105.    l.add(i) ;
  106.    cout << "l is " << l << endl ;
  107.    cout << "head of l is " << l.head() << endl ;
  108.    List<int> m = l.tail() ;
  109.    List<int> o ;
  110.    o = m;
  111.    i=4;
  112.    m.add(i);
  113.    cout << "m is " << m << endl ;
  114.    cout << "o is " << o << endl ;
  115.    List<char> clist ;                     // Character list
  116.    char ch;
  117.    ch='a';
  118.    clist.add(ch);
  119.    ch='b';
  120.    clist.add(ch);
  121.    cout << clist << endl ;
  122.    List<string> ls ;                      // string List
  123.    ls.add(string("hello")) ;
  124.    ls.add(string("world")) ;
  125.    cout << "List of strings" << endl ;
  126.    cout << ls << endl ;
  127.    List<List<int> > listlist ;    // List of lists of integer. Notice that lists of lists are possible
  128.    listlist.add(o) ;
  129.    listlist.add(m) ;
  130.    cout << "List of lists of intsn" ;
  131.    cout << listlist << endl ;
  132.    List<List<List<int> > > lllist ;    // List of lists of lists of integer
  133.    lllist.add(listlist) ;
  134.    lllist.add(listlist) ;
  135.    cout << "List of lists of lists of intsn" ;
  136.    cout << lllist << "n" ;
  137.    List<List<string> > slist ;                      // List of list of strings
  138.    slist.add(ls) ;
  139.    slist.add(ls) ;
  140.    cout << "List of lists of stringsn" ;
  141.    cout << slist << "n" ;
  142.    return 0 ;
  143. }