binSort.cppnode
/* * 這裏是主函數的執行文件 * binSort.cpp */ #include<iostream> #include"studentrecord1.h" #include"chainwithiterator.h" #include"myexceptions.h" void binSort(chain<studentRecord>& theChain,int range) { chain<studentRecord> *bin; bin = new chain<studentRecord> [range+1]; int numberOfElements = theChain.size(); for(int i = 1;i <= numberOfElements;i++) { studentRecord x = theChain.get(0); theChain.erase(0); //在排序後的鏈表插入新元素,且在鏈表頭插入 bin[x.score].insert(0,x); } //從bin中收集元素,在存儲到theChain鏈表中 for(int j = range;j>=0;j--) { while(!bin[j].empty()) { studentRecord x = bin[j].get(0); bin[j].erase(0); theChain.insert(0,x); } } delete [] bin; } int main(int argc, char *argv[]) { studentRecord s; chain<studentRecord> c; for(int i = 1;i <= 20;i++) { s.score = i / 2; s.name = new string(s.score,'a'); c.insert(0,s); } cout<<"The unsorted chain is"<<endl; cout <<" "<< c <<endl; binSort(c,10); cout <<"The sorted chain is" <<endl; cout <<" "<< c <<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
chainWithIterator.h函數
/* *鏈表迭代器的定義 * chainWithIterator.h */ #ifndef CHAINWITHITERATOR_H #define CHAINWITHITERATOR_H #include<iostream> #include<sstream> #include<string> #include"linearlist.h" #include"chainnode.h" #include"myexceptions.h" using namespace std; class linkedDigraph; template<class T> class linkedWDigraph; template<class T> class chain : public linearList<T> { friend linkedDigraph; friend linkedWDigraph<int>; friend linkedWDigraph<float>; friend linkedWDigraph<double>; public : /* * 構造函數,複製構造函數,析構函數 */ chain(int initialCapaticy = 10); chain(const chain<T>&); ~chain(); //抽象函數的具體聲明 bool empty() const{return listSize == 0;} int size() const{return listSize;} T& get(int theIndex) const; int indexOf(const T &theElement) const; void insert(int theIndex,const T& theElement); void erase(int theIndex); void output(ostream& out) const; //迭代器的開始和結束 class iterator; iterator begin() {return iterator(firstNode);} iterator end(){return iterator(NULL);} //對chain的迭代器 class iterator { public: typedef forward_iterator_tag iterator_category; typedef T value_type; typedef ptrdiff_t difference_type; typedef T* pointer; typedef T& reference; //構造函數 iterator(chainNode<T>* theNode = NULL) { node = theNode; } // T& operator*() const{return node->element;} T* operator->()const{return &node->element;} //對++的重載 iterator& operator++()//前加 { node = node->next; return *this; } iterator operator++(int)//後加 { iterator old = *this; node = node->next; return old; } bool operator != (const iterator right) const { return node != right.node; } bool operator == (const iterator right) const { return node == right.node; } protected: chainNode<T>* node; }; protected: void checkIndex(int theIndex) const; chainNode<T>* firstNode;//指向鏈表彙總首節點 int listSize;//元素數量 }; /* * 類中函數的具體實現 */ //構造函數 template<class T> chain<T>::chain(int initialCapaticy) { if(initialCapaticy < 1) { ostringstream s; s << "Initial capacity = "<< initialCapaticy <<"Must be > 0"; throw illegalParameterValue(s.str()); } firstNode = NULL; listSize = 0; } //複製構造函數 template<class T> chain<T>::chain(const chain<T>& theList) { listSize = theList.listSize; //鏈表爲空 if(listSize == 0) { firstNode = NULL; return; } //鏈表非空 chainNode<T>* sourceNode = theList.firstNode; firstNode = new chainNode<T>(sourceNode->element);//複製第一個元素到theList鏈表 sourceNode = sourceNode->next; chainNode<T>* targetNode = firstNode; while(sourceNode != NULL) { targetNode->next = new chainNode<T>(sourceNode->element); targetNode = targetNode->next; sourceNode = sourceNode->next; } targetNode->next = NULL;//結束鏈表 } //析構函數,刪除鏈表中全部節點 template<class T> chain<T>::~chain() { chainNode<T> *nextNode; while(firstNode != NULL) { nextNode = firstNode->next; delete firstNode; firstNode = nextNode; } } //函數:checkIndex() //用於檢查元素索引是否合法 template<class T> void chain<T>::checkIndex(int theIndex) const { if(theIndex < 0 || theIndex >= listSize) { ostringstream s; s <<"index = "<<theIndex <<" size = " <<listSize; throw illegalIndex(s.str()); } } //函數:get(int theIndex) //獲取索引爲theIndex的元素 template<class T> T& chain<T>::get(int theIndex) const { checkIndex(theIndex); chainNode<T>* currentNode = firstNode; for(int i = 0;i < theIndex;i++) currentNode = currentNode->next; return currentNode->element; } //返回某元素的索引 template<class T> int chain<T>::indexOf(const T &theElement) const { chainNode<T>* currentNode = firstNode; int index = 0; while(currentNode != NULL && currentNode->element != theElement) { currentNode = currentNode->next; index++; } if(currentNode == NULL) return -1; else return index; } //刪除索引爲theIndex 的元素 template<class T> void chain<T>::erase(int theIndex) { checkIndex(theIndex); chainNode<T>* deleteNode; if(theIndex == 0) { deleteNode = firstNode; firstNode = firstNode->next; } else { chainNode<T>* p = firstNode; for(int i = 0;i< theIndex-1;i++)//此處和書上寫法不一樣,斯覺得書上的較繁瑣 { p = p->next; } deleteNode = p->next; p->next = p->next->next; } listSize--; delete deleteNode; } //在某索引位置插入一個元素 template<class T> void chain<T>::insert(int theIndex, const T &theElement) { if(theIndex < 0 || theIndex > listSize) { ostringstream s; s <<"index = " << theIndex <<" size = "<<listSize; throw illegalIndex(s.str()); } if(theIndex == 0) firstNode = new chainNode<T>(theElement,firstNode); else { chainNode<T>* p = firstNode; for(int i = 0;i<theIndex -1;i++) p = p->next; p->next = new chainNode<T>(theElement,p->next); } listSize++; } //將鏈表元素送到輸出流 template<class T> void chain<T>::output(ostream &out) const { for(chainNode<T>* currentNode = firstNode; currentNode != NULL; currentNode = currentNode->next) out<< currentNode->element << " "; } //重載<< template<class T> ostream& operator << (ostream& out,const chain<T>& x) { x.output(out); return out; } #endif // CHAINWITHITERATOR_H
linearList.hthis
/* * 鏈表的抽象類 * linearList.h */ #ifndef linearList_ #define linearList_ #include<iostream> using namespace std; template<class T> class linearList { public: virtual ~linearList() {};//析構函數 virtual bool empty() const = 0;//判斷鏈表是否爲空 virtual int size() const = 0;//返回鏈表元素個數 virtual T& get(int theIndex) const = 0;//返回索引theIndex指代元素 virtual int indexOf(const T& theElement) const = 0;//返回某元素索引 virtual void erase(int theIndex) = 0;//刪除索引爲theIndex的元素 virtual void insert(int theIndex,const T& theElelemt) = 0; virtual void output(ostream& out) const = 0; }; #endif
myExceptions.hspa
/* * 異常類 * myExceptions.h */ #ifndef MYEXCEPTIONS_H #define MYEXCEPTIONS_H #include<string> using namespace std; //不合法的參數值 class illegalParameterValue { public: illegalParameterValue(string theMessage = "Illegal parameter 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
studentRecord1.hcode
/* * 記錄學生信息,包括姓名和分數 * studentRecord1.h */ #ifndef STUDENTRECORD1_H #define STUDENTRECORD1_H /* * 學生信息的結構體定義 * studentRecord1.h */ #include <string> using namespace std; struct studentRecord { int score; string *name; int operator != (studentRecord x) const { return (score != x.score); } }; ostream& operator<<(ostream& out,const studentRecord& x) { out << x.score <<' ' <<*x.name <<endl; return out; } #endif // STUDENTRECORD1_H