首页 > 代码库 > List

List

实在是受不了了,作业被我不小心格式化了,所以把作业存到了博客上,哎

VirtualListClass

 1 #ifndef VIRTUALLISTCLASS 2 #define VIRTUALLISTCLASS 3 template <typename T> class VirtualListClass  { // VirtualListClass ADT 4 private: 5     void operator = (const VirtualListClass&) {} 6     // Protect assignment 7     VirtualListClass(const VirtualListClass&) {} 8     // Protect copy constructor 9 public:10     VirtualListClass() {}11     // Default constructor12 13     virtual ~VirtualListClass() {} // Base destructor14 15     // Clear contents from the VirtualListClass, to make it empty.16     virtual void clear() = 0;17 18     // put an element at the total location.19     // item: The element to be inserted20     virtual void push_back(const T& item) = 0;21 22     // Remove and return the element at pos.23     // pos is the position24     // Return: the element that was removed.25     virtual T remove(int pos) = 0;26 27     // insert a element at some position28     // The position to be insert29     virtual void insert(const T& item,int pos) = 0;30 31     // Return: The number of elements in the VirtualListClass.32     virtual int length() const = 0;33 34     // Return a element.35     // pos is the position of the element.36     virtual const T& getValue(int pos) const = 0;37 };38 39 #endif
View Code

ListClass

 1 #include "VirtualListClass.h" 2  3 #ifndef LISTCLASS 4 #define LISTCLASS 5  6 const int MAXSIZE = 0xfffffe; 7  8 template <typename T> // Array-based list implementation 9 class ListClass : public VirtualListClass<T> {10 private:11     int maxSize;12     // Maximum size of list13 14     int listSizeNow;15     // Number of list items now16 17     int curr;18     // Position of current element19 20     T* listArray;21     // Array holding list elements22 public:23     ListClass(int size = MAXSIZE) { // Constructor24         maxSize = size;25         listSizeNow = curr = 0;26         listArray = new T[maxSize];27     }28     ~ListClass() { delete [] listArray; } // Destructor29 30     void clear() {31         delete [] listArray;32         listSizeNow = curr = 0;33         listArray = new T[maxSize];34     }35 36     void push_back(const T& item) {37         listArray[curr++] = item;38         listSizeNow++;39         // Increase list size40     }41 42     // Remove and return the current element.43     T remove(int pos) {44         T it = listArray[pos];45         // Copy the element46         for(int i=pos; i<listSizeNow-1; i++) // Shift them down47         listArray[i] = listArray[i+1];48         listSizeNow--;49         curr--;50         // Decrease list size51         return it;52     }53 54     void insert(const T& item,int pos) {55         for (int i = listSizeNow;i > pos;i--) {56             listArray[i] = listArray[i - 1];57         }58         listArray[pos] = item;59         curr ++;60         listSizeNow++;61         // Increase list size62     }63 64     int length() const { return listSizeNow; }65 66     const T& getValue(int pos) const { // Return current element67         return listArray[pos];68     }69 70     T operator [] (const int pos) const {71         return listArray[pos];72     }73 74 };75 76 #endif
View Code

 

List