?? list.h.old
字號:
#ifndef List_H#define List_H#include "defs.H"const long DEFAULT_LIST_CAPACITY = 200;template<class Item> class Iterator {public: virtual void First() = 0; virtual void Next() = 0; virtual bool IsDone() const = 0; virtual Item CurrentItem() const = 0;protected: Iterator();};template<class Item> class List {public: List(long size = DEFAULT_LIST_CAPACITY); List(List&); List& operator=(List&); ~List(); //---- accessing long Count() const; Item& Get(long index) const; Item& First() const; Item& Last() const; bool Includes(const Item& anItem) const; //---- iteration Iterator<Item>* CreateIterator(); //---- modifying void Append(const Item& anItem); void Prepend(const Item& anItem); void Remove(const Item& anItem); void RemoveFirst(); void RemoveLast(); void RemoveAll(); //---- stack interface Item& Top() const; void Push(const Item& anItem); Item& Pop(); private: void RemoveAt(long index); private: Item* _items; long _size; long _count; };//---- class ListIterator --------------------------------------------------template<class Item> class ListIterator: public Iterator<Item> {public: ListIterator(List<Item>* aList); virtual void First(); virtual void Next(); virtual bool IsDone() const; virtual Item CurrentItem() const; private: List<Item>* _list; long _current; long _end;};//---- class Iterator implementation --------------------------------------------template<class Item> Iterator<Item>::Iterator () {}//---- class List implementation ------------------------------------------------template<class Item> List<Item>::List(long size) : _size(size), _count(0) { _items = new Item[size]; }template<class Item> List<Item>::~List () { delete _items;} template<class Item> long List<Item>::Count () const { return _count;}template<class Item> Item& List<Item>::Get (long index) const { if (_count <= _size) return _items[index]; else ; // throw RangeError exception}template<class Item> void List<Item>::Append (const Item& anItem) { if (_count <= _size) { _items[_count] = anItem; _count++; }}template<class Item> void List<Item>::Prepend (const Item& anItem) { if (_count <= _size) { for (long i = _count-1; i >= 0; i--) _items[i+1] = _items[i]; _items[0] = anItem; _count++; }}template<class Item> Iterator<Item>* List<Item>::CreateIterator () { return new ListIterator<Item>(*this);}template<class Item> Item& List<Item>::First () const { return Get(0);}template<class Item> Item& List<Item>::Last () const { return Get(Count() -1);}template<class Item> bool List<Item>::Includes (const Item& anItem) const { for (long i = 0; i < Count(); i++) { if (_items[i] == anItem) { return true; } } return false;}template<class Item> void List<Item>::Remove (const Item& anItem) { for (long i = 0; i < Count(); i++) { if (_items[i] == anItem) { RemoveAt(i); } }}template<class Item> void List<Item>::RemoveAll () { _count = 0;}template<class Item> void List<Item>::RemoveFirst () { RemoveAt(0);}template<class Item> void List<Item>::RemoveLast () { RemoveAt(Count()-1);}template<class Item> void List<Item>::Push (const Item& anItem) { Append(anItem);}template<class Item> Item& List<Item>::Pop (){ Item& top = Last(); RemoveLast(); return top; }template<class Item> Item& List<Item>::Top () const { return Last();}template<class Item> void List<Item>::RemoveAt (long index) { if (index < 0 || index >= Count() ) cout << "Throw RangeException\n"; for (long i = index; i < Count() -1; i++) _items[i] = _items[i+1]; _count--;}//---- class ListIterator Implementation -------------------------------------------template<class Item> ListIterator<Item>::ListIterator (List<Item>& aList) : _list(&aList), _current(0) { _end = aList.Count(); }template<class Item> void ListIterator<Item>::First () { _current = 0;}template<class Item> void ListIterator<Item>::Next () { _current++;}template<class Item> bool ListIterator<Item>::IsDone () const { return _current >= _end;}template<class Item> Item ListIterator<Item>::CurrentItem () const { return _list->Get(_current);}#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -