資料結構之迴圈佇列(SeqQueue)原始碼
阿新 • • 發佈:2019-01-01
SeqQueue.h檔案
#pragma once #include<assert.h> #include<iostream> #define defaultSize 20 template<class T> class SeqQueue { public: SeqQueue(int sz = defaultSize); //預設建構函式,構建一個大小為20的佇列 SeqQueue(SeqQueue<T>& queuePar); //複製建構函式 ~SeqQueue(); //解構函式,釋放_pelemet的記憶體 SeqQueue<T>& operator = (const SeqQueue<T>& queuePar);//賦值運算子 bool isEmpty()const; //判斷空否 bool isFull()const; //判斷滿否 bool enQueue(const T& par); //入隊 bool deQueue(T& par); //出隊 int getSize(); //獲取元素個數 void traverse()const; //遍歷輸出函式 private: void init(SeqQueue<T>& queuePar); //初始化函式 void makeEmpty(); //置空函式 int _rear; //隊尾指標 int _front; //隊頭指標 T* _pelemet; //存放佇列元素的素組 int _maxSize; }; //預設建構函式,構建一個大小為20的佇列 template<class T> SeqQueue<T>::SeqQueue(int sz /*= defaultSize*/) { _rear = _front = 0; //是隊尾與隊頭指標同時指向空 _maxSize = sz; //引數賦值給類成員變數元素個數最大值 _pelemet = new T[_maxSize]; //為陣列屬性開闢空間 assert(_pelemet != nullptr); //斷言所分配記憶體非空 } //初始化函式 template<class T> void SeqQueue<T>::init(SeqQueue<T>& queuePar) { //分別拷貝屬性值 this->_front = queuePar._front; this->_rear = queuePar._rear; this->_maxSize = queuePar._maxSize; //為本物件的陣列元素開闢空間 this->_pelemet = new T[this->_maxSize]; //斷言是否成功分配空間 assert(this->_pelemet != nullptr); int tempFront = queuePar._front; while (tempFront % queuePar._maxSize != queuePar._rear) { //如果未達到隊尾 this->_pelemet[tempFront] = queuePar._pelemet[tempFront]; tempFront++; } } //複製建構函式 template<class T> SeqQueue<T>::SeqQueue(SeqQueue<T>& queuePar) { init(queuePar); //直接呼叫初始化函式 } //賦值運算子 template<class T> SeqQueue<T>& SeqQueue<T>::operator = (const SeqQueue<T>& queuePar) { init(queuePar); //直接呼叫初始化函式 } //解構函式,釋放_pelemet的記憶體 template<class T> SeqQueue<T>::~SeqQueue() { makeEmpty(); //直接呼叫清空函式 } //判斷空否 template<class T> bool SeqQueue<T>::isEmpty()const { return (_rear == _front) ? true : false; //如果隊尾指標和隊頭指標一致,就為空 } //判斷滿否 template<class T> bool SeqQueue<T>::isFull()const { //如果隊尾指標加一除以最大容量個數等於隊頭指標,就為滿 return((_rear + 1) % _maxSize == _front) ? true : false; } //入隊 template<class T> bool SeqQueue<T>::enQueue(const T& par) { if (isFull()) //如果佇列已滿,在不能進行插入 return false; //如果未滿 _pelemet[_rear] = par; //在隊尾插入元素 _rear = (_rear + 1) % _maxSize; //隊尾指標加一 return true; } //出隊 template<class T> bool SeqQueue<T>::deQueue(T& par) { if (isEmpty()) //如果佇列為空,則不能進行出隊操作 return false; par = _pelemet[_front]; //將隊頭指標賦值給引數 _front = (_front + 1) % _maxSize;//隊頭指標加一 return true; } //獲取元素個數 template<class T> int SeqQueue<T>::getSize() { return (_rear - _front + _maxSize) % _maxSize; } //置空函式 template<class T> void SeqQueue<T>::makeEmpty() { if (!isEmpty()) { //如果不為空就刪除記憶體 delete[]_pelemet; _rear = _front = 0; } } //遍歷輸出函式 template<class T> void SeqQueue<T>::traverse() const{ int tempFront = _front; while(tempFront % _maxSize != _rear){ //如果還沒有達到隊尾 cout << _pelemet[tempFront] << " "; tempFront++; } }
main.cpp檔案(測試檔案)
#include"SeqQueue.h" using namespace std; int main() { //測試建構函式 SeqQueue<int> test(6); //測試入隊 test.enQueue(1); test.enQueue(2); test.enQueue(3); test.enQueue(4); //test.enQueue(5); if (test.enQueue(5)) cout << "插入5成功!" << endl; else cout << "插入5失敗!" << endl; //test.enQueue(6); if (test.enQueue(6)) cout << "插入6成功!" << endl; else cout << "插入6失敗!" << endl; if (test.enQueue(7)) cout << "插入7成功!" << endl; else cout << "插入7失敗!" << endl; //測試出隊函式 int temp; if (test.deQueue(temp)) cout << "彈出隊首元素" << temp << "成功!" << endl; else cout << "彈出失敗!" << endl; //測試獲取元素個數函式 cout << "元素個數:" << test.getSize() << endl; //測試遍歷輸出函式 test.enQueue(6); test.enQueue(7); test.traverse(); return 0; }