1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| class Link
{
public:
Link *nxt;
Link *prev;
T element;
Link(Link *p = 0, Link *n = 0){
prev= p;
nxt = n;
if (prev)
prev->nxt = this;
if (nxt)
nxt->prev = this;
}
~Link(){
if(prev)
prev->nxt = nxt;
if(nxt)
nxt->prev = prev;
}
};
protected:
ListIterator(){ head = curr = 0; }
Link *head;
Link *curr;
friend class List< T >;
friend class FullList< T >;
};
template< class T > class List: public ListIterator< T >
{
public:
List();
List(const List &l);
~List(){
flush();
delete head;
}
List & operator = (const List &l);
List & operator = (const ListIterator< T >&li);
List operator + (const List &l) const;
List & operator +=(const List &l);
T & operator [](int index);
void insert(const T &el);
void insert(ListIterator< T > &li, const T &el);
int nElements() const;
void remove();
void remove(ListIterator< T > &li);
void flush();
protected:
int nels;
T **ptrArray;
};
template< class T > class FullList : public List< T >
{
public:
FullList():List< T >(){}
FullList(const List< T > &list):List< T >(list){}
FullList & operator = (const ListIterator< T > &li);
int search(const T &el);
int search(ListIterator< T >&, const T &el) const;
void write(ostream &out = cout) const;
void read(istream &in = cin);
friend ostream & operator<<(ostream &out, const FullList< T > &l);
friend istream & operator>>(istream &in, FullList< T > &l);
};
template< class T >
inline T& ListIterator< T >::operator()() const
{
if (curr == head)
throw Exception("No current element!");
return curr->element;
}
template< class T >
inline ListIterator< T >& ListIterator< T >::operator++()
{
curr = curr->nxt;
return *this;
}
template< class T >
inline ListIterator< T >& ListIterator< T >::operator--()
{
curr = curr->prev;
return *this;
} |
Partager