循環鏈表的原理很簡單,這裏就不在贅述。下面是循環鏈表的測試代碼:
主函數所在文件:
circularlistwithheader.cppnode
/* * 主函數 */ #include<iostream> #include "circularlistwithheader.h" using namespace std; int main(int argc, char *argv[]) { //測試構造函數 circularListWithHeader<int>y,z; cout<<"Initial size of y and z = " <<y.size()<<", " <<z.size() <<endl; //測試insert()函數 y.insert(0,2); y.insert(1,6); cout<<"after insert 2 and 6:";y.output(cout);cout<<endl; y.insert(0,1); y.insert(2,4); cout<<"after insert 1 and 4:";y.output(cout);cout<<endl; y.insert(3,5); y.insert(2,3); cout<<"after insert 5 and 3:";y.output(cout);cout<<endl; //測試size()函數 cout<<"size of y = "<<y.size()<<endl; //測試indexOf()函數 int index = y.indexOf(5); if(index < 0) { cout << "4 not fouond" <<endl; } { cout << "The index of 4 is" <<index <<endl; } index = y.indexOf(7); if(index < 0) { cout << "7 not found" <<endl; } else { cout << "The index of 7 is " <<index <<endl; } return 0; }
chainNode.hios
/* * chainNode.h * 鏈表節點定義 * */ #ifndef CHAINNODE_H #define CHAINNODE_H template<class T> struct chainNode { //數據成員 T element; chainNode<T> *next; //方法 chainNode(){} chainNode(const T& element) { this->element = element; } chainNode(const T& element,chainNode<T>* next) { this->element = element; this->next = next; } }; #endif // CHAINNODE_H
circularListWithHeader.h函數
/* * circularListWithHeader.h * 循環鏈表實體類定義 */ #ifndef CIRCULARLISTWITHHEADER_H #define CIRCULARLISTWITHHEADER_H #include<iostream> #include<sstream> #include<string> #include "chainnode.h" #include "myexceptions.h" using namespace std; template<class T> class circularListWithHeader { public: //構造函數 circularListWithHeader(); /* * 一些其餘函數 */ //返回鏈表中元素個數 int size() const {return listSize;} int indexOf(const T& theElement) const; void insert(int theIndex,const T& theElement); void output(ostream& out) const; protected: //檢查索引是否合法 void checkIndex(int theIndex) const; chainNode<T>* headerNode;//指向頭節點 int listSize;//元素個數 }; /* * 類中各個函數的具體實現 * */ template<class T> circularListWithHeader<T>::circularListWithHeader() { headerNode = new chainNode<T>(); headerNode->next = headerNode; listSize = 0; } template<class T> void circularListWithHeader<T>::checkIndex(int theIndex) const { if(theIndex < 0 || theIndex >= listSize) { ostringstream s; s <<"index = "<<theIndex<<" size = "<<listSize; throw illegalIndex(s.str()); } } template<class T> int circularListWithHeader<T>::indexOf(const T &theElement) const { headerNode->element = theElement; chainNode<T>* currentNode = headerNode->next; int index = 0; while(currentNode->element != theElement) { currentNode = currentNode->next; index++; } if(currentNode == headerNode) return -1; else return index; } template<class T> void circularListWithHeader<T>::insert(int theIndex, const T &theElement) { if(theIndex < 0 || theIndex > listSize) { ostringstream s; s<<"index = "<<theIndex<<" size = "<<listSize; throw illegalIndex(s.str()); } chainNode<T>* p = headerNode; for(int i = 0;i< theIndex;i++) { p = p->next; } p->next = new chainNode<T>(theElement,p->next); listSize++; } template<class T> void circularListWithHeader<T>::output(ostream &out) const { for(chainNode<T>* currentNode = headerNode->next; currentNode != headerNode; currentNode = currentNode->next) out<<currentNode->element<<" "; } //<<的重載 template<class T> ostream& operator <<(ostream& out,const circularListWithHeader<T>& x) { x.output(out); return out; } #endif // CIRCULARLISTWITHHEADER_H
myExceptions.h測試
/* * myExceptions.h * 這個類包含類對各類異常的處理類 */ #ifndef MYEXCEPTIONS_H #define MYEXCEPTIONS_H #include<string> using namespace std; //不合法的參數值 class illegalParameterValue { public: illegalParameterValue(string theMessage = "Illegal patameter value") { message = theMessage; } void outputMessage() { cout<<message<<endl; } private: string message; }; //輸入數據不合法 class illegalInputData { public: illegalInputData(string theMessage = "Illegal data input") { message = theMessage; } void outputMessage() { cout <<message <<endl; } private: string message; }; //索引不合法 class illegalIndex { public: illegalIndex(string theMessage = "Illegal index") { message = theMessage; } void outputMessage() { cout<<message<<endl; } private: string message; }; #endif // MYEXCEPTIONS_H