vectorList.hios
#ifndef VECTORLIST_H #define VECTORLIST_H #include<iostream> #include"linearlist.h" #include<vector> #include<myexceptions.h> using namespace std; template<class T> class vectorList : public linearList<T> { public: //構造函數 vectorList(int initialCapacity = 0); //複製構造函數 vectorList(const vectorList<T>&); //析構函數 ~vectorList() { delete element; } /* * 類linearList中抽象方法的實現 */ //判斷是否表空 bool empty() const { return element->empty(); } //返回表內元素個數 int size() const { return (int)element->size(); } //返回索引爲theIndex的元素 T& get(int theIndex) const; //返回元素theElement的索引 int indexOf(const T &theElement) const; //刪除索引爲theIndex的元素 void erase(int theIndex); //在索引爲theIndex的位置插入元素theElement void insert(int theIndex, const T &theElement); //將線性表元素放入輸出流 void output(ostream &out) const; /* * 增長的方法 */ int capacity() const { return (int)element->capacity(); } /* * 線性表的起始和結束位置的迭代器 */ typedef typename vector<T>::iterator iterator; iterator begin(){return element->begin();} iterator end(){return element->end();} protected: void checkIndex(int theIndex) const; vector<T>* element;//存儲線性表元素的向量 }; /* * 構造函數和複製構造函數的實現 */ template<class T> vectorList<T>::vectorList(int initialCapacity) { //構造函數 if(initialCapacity < 1) { ostringstream s; s<<"Initial capacity = "<<initialCapacity<<"Must be > 0"; throw illegalParameterValue(s.str); } element = new vector<T>;//建立向量爲0的空向量 element->reserve(initialCapacity);//vector的容量從0增長到initialCapacity } template<class T> vectorList<T>::vectorList(const vectorList<T> &theList) { //複製構造函數 element = new vector<T>(*theList.element); } //刪除元素 template<class T> void vectorList<T>::erase(int theIndex) { checkIndex(theIndex); element->erase(begin()+theIndex); } //插入元素 template<class T> void vectorList<T>::insert(int theIndex, const T &theElement) { if(theIndex < 0 || theIndex > size()) { //無效索引 ostringstream s; s<<"index = "<<theIndex <<" size = "<<size(); throw illegalIndex(s.str()); } element->insert(element->begin()+theIndex,theElement); } #endif // VECTORLIST_H
myexceptions.h函數
#ifndef MYEXCEPTIONS_H #define MYEXCEPTIONS_H #include <string> using namespace std; // illegal parameter value class illegalParameterValue { public: illegalParameterValue(string theMessage = "Illegal parameter value") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message; }; // illegal input data class illegalInputData { public: illegalInputData(string theMessage = "Illegal data input") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message; }; // illegal index class illegalIndex { public: illegalIndex(string theMessage = "Illegal index") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message; }; // matrix index out of bounds class matrixIndexOutOfBounds { public: matrixIndexOutOfBounds (string theMessage = "Matrix index out of bounds") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message; }; // matrix size mismatch class matrixSizeMismatch { public: matrixSizeMismatch(string theMessage = "The size of the two matrics doesn't match") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message; }; // stack is empty class stackEmpty { public: stackEmpty(string theMessage = "Invalid operation on empty stack") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message; }; // queue is empty class queueEmpty { public: queueEmpty(string theMessage = "Invalid operation on empty queue") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message; }; // hash table is full class hashTableFull { public: hashTableFull(string theMessage = "The hash table is full") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message; }; // edge weight undefined class undefinedEdgeWeight { public: undefinedEdgeWeight(string theMessage = "No edge weights defined") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message; }; // method undefined class undefinedMethod { public: undefinedMethod(string theMessage = "This method is undefined") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message; }; #endif // MYEXCEPTIONS_H
linearList.hspa
#ifndef LINEARLIST_H #define LINEARLIST_H // LINEARLIST_H // abstract class linearList // abstract data type specification for linear list data structure // all methods are pure virtual functions #include <iostream> using namespace std; template<class T> class linearList { public: virtual ~linearList() {}; virtual bool empty() const = 0; // return true iff list is empty virtual int size() const = 0; // return number of elements in list virtual T& get(int theIndex) const = 0; // return element whose index is theIndex virtual int indexOf(const T& theElement) const = 0; // return index of first occurence of theElement virtual void erase(int theIndex) = 0; // remove the element whose index is theIndex virtual void insert(int theIndex, const T& theElement) = 0; // insert theElement so that its index is theIndex virtual void output(ostream& out) const = 0; // insert list into stream out }; #endif