- //文件名:CHAPTER3-16.cpp
- // Template class for storing list elements
- #include <iostream>
- #include <string>
- using namespace std;
- template <class T> // Use template keyword
- class ListElement //定义类ListElement,用于表示list对象
- {
- public:
- T data;
- ListElement<T> * next ;
- ListElement(T& i_d, ListElement <T>* i_n)
- : data(i_d),next(i_n) { }
- ListElement<T>* copy() // copy includes all next elements
- { return new ListElement(data,(next?next->copy():0)); }
- };
- template <class T> class ListIterator //定义类ListIterator,用于访问和操作list对象
- { public :
- //ListIterator(List<T>& l) ;
- T operator()() ;
- int operator++() ;
- int operator!() ;
- public:
- ListElement<T>* rep ;
- };
- template <class T>T ListIterator<T>::operator() ()
- { if (rep) return rep->data;
- else
- {
- T tmp ; return tmp ; // Default value
- }
- }
- template <class T>int ListIterator<T>::operator++(){ if (rep) rep = rep->next ; return (rep != 0) ;}
- template <class T>int ListIterator<T>::operator!(){ return (rep != 0) ;}
- template <class T>class List //定义类List
- {public:
- friend class ListIterator<T> ;
- List();
- List(const List&);
- ~List();
- List<T>& operator=(const List<T>&);
- // typical list ops
- T head();
- List<T> tail();
- void add(T&);
- friend ostream& operator<<(ostream&, const List<T>&);
- public:
- void clear() ; // Delete all list elements
- ListElement<T>* rep ;
- };
- // Default Constructor
- template <class T>
- List<T>::List(){ rep = 0 ; }
- // Copy Constructor
- template <class T>List<T>::List(const List<T>& l){ rep = l.rep ? l.rep->copy() : 0 ;}
- // Overloaded assignment operator
- template <class T>List<T>& List<T>::operator=(const List<T>& l)
- { if (rep != l.rep) { clear() ; rep = l.rep ? l.rep->copy() : 0 ; }
- return *this ;
- }
- // Destructor
- template <class T>List<T>::~List(){ clear() ;}
- // Delete representation
- template <class T>
- void List<T>::clear()
- { while (rep)
- { ListElement<T>* tmp = rep ;
- rep = rep->next ;
- delete tmp ;
- }
- rep = 0 ;
- }
- // Add element to front of list
- template <class T>void List<T>::add(T& i){ rep = new ListElement<T>(i,rep) ;}
- // Return head of list or default value of type T
- template <class T>T List<T>::head(){ if (rep) return rep->data ; else {T tmp ; return tmp ;}}
- // Return tail of list or empty list
- template <class T>List<T> List<T>::tail()
- { List<T> tmp ;
- if (rep)
- if (rep->next) tmp.rep = rep->next->copy() ;
- return tmp ;
- }
- // Output operator
- template <class T>ostream& operator<<(ostream& os, const List<T>& l)
- { if (l.rep)
- { ListElement<T>* p = l.rep ;
- os << "( " ;
- while (p){ os << p->data << " " ; p = p->next ; }
- os << ")n" ;
- }
- else
- os << "Empty listn" ;
- return os ;
- }
- int main()
- {
- List<int> l ; // Integer list
- cout << l ;
- int i=1;
- l.add(i) ;
- i=2;
- l.add(i) ;
- i=3;
- l.add(i) ;
- cout << "l is " << l << endl ;
- cout << "head of l is " << l.head() << endl ;
- List<int> m = l.tail() ;
- List<int> o ;
- o = m;
- i=4;
- m.add(i);
- cout << "m is " << m << endl ;
- cout << "o is " << o << endl ;
- List<char> clist ; // Character list
- char ch;
- ch='a';
- clist.add(ch);
- ch='b';
- clist.add(ch);
- cout << clist << endl ;
- List<string> ls ; // string List
- ls.add(string("hello")) ;
- ls.add(string("world")) ;
- cout << "List of strings" << endl ;
- cout << ls << endl ;
- List<List<int> > listlist ; // List of lists of integer. Notice that lists of lists are possible
- listlist.add(o) ;
- listlist.add(m) ;
- cout << "List of lists of intsn" ;
- cout << listlist << endl ;
- List<List<List<int> > > lllist ; // List of lists of lists of integer
- lllist.add(listlist) ;
- lllist.add(listlist) ;
- cout << "List of lists of lists of intsn" ;
- cout << lllist << "n" ;
- List<List<string> > slist ; // List of list of strings
- slist.add(ls) ;
- slist.add(ls) ;
- cout << "List of lists of stringsn" ;
- cout << slist << "n" ;
- return 0 ;
- }