新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C++怎么實現(xiàn)循環(huán)隊列
這篇文章將為大家詳細講解有關(guān)C++怎么實現(xiàn)循環(huán)隊列,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
為大新等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及大新網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都網(wǎng)站設計、成都網(wǎng)站建設、大新網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
具體內(nèi)容如下
circularQueue.h
#pragma once #pragma once #ifndef CIRCULARQUEUE_H #define CIRCULARQUEUE_H #include#include using std::cout; using std::cin; using std::endl; using std::ostream; template class cirQueue; template class cirQueue { public: cirQueue(int sz); ~cirQueue(); void push(const T& elem);//進隊 void pop(T& elem);//出隊 bool empty();//查看隊列是否為空 int getSize();//返回隊列中元素的個數(shù) void clearQueue();//清空隊列中的元素 void print();//打印隊列中的元素 int getfront() { return front; } int getrear() { return rear; } bool getTop(T& elem);//讀取隊列首個元素 template friend ostream& operator<<(ostream& os, cirQueue & queue); private: bool _full()const;//判斷隊列是否已滿 int maxsize;//隊列最大的空間 T* element;//存放于隊列中的元素數(shù)組 int front;//模擬隊頭指針 int rear;//模擬隊尾指針 }; template cirQueue ::cirQueue(int sz) { maxsize = sz; element = new T[maxsize]; if (element == nullptr) cout << "內(nèi)存分配失敗" << endl; front = 0; rear = 0; } template cirQueue ::~cirQueue() { if (element != nullptr) delete element; } //進隊 template void cirQueue ::push(const T& elem) {//需要保證隊尾指針位置與首個元素相差一個位置 if (rear > (maxsize - 1)) rear -= maxsize ; if (front > (maxsize - 1)) front -= maxsize ; if (!_full()) {//隊列未滿的情況 element[rear++] = elem;//隊尾向后移動一位 //++rear; } else { cout << "隊列已滿,不能插入!" << endl; return; } } //出隊 template void cirQueue ::pop(T& elem) { if (rear > (maxsize - 1)) rear -= (maxsize - 1); if (front > (maxsize - 1)) front -= (maxsize - 1); if (!empty()) {//隊列未空的情況 elem = element[front++];//隊頭向后移動一位 element[front - 1] = 0;//置零 } else { cout << "隊列已空!" << endl; return; } } //查看隊列是否為空 template bool cirQueue ::empty() { if (front == rear)//待定 return true; return false; } //返回隊列中元素的個數(shù) template int cirQueue ::getSize() { int num = 0; if (front <= rear) return rear - front; else return maxsize - front + rear + 1; } //清空隊列中的元素 template void cirQueue ::clearQueue() { if (!empty()) { int Index = 0; while (front < rear) {//front逼近rear element[front++] = 0; if (front == rear) return; } if (rear < front) { while (front <= maxsize - 1)//刪除front至數(shù)組尾端的數(shù)據(jù) element[front++] = 0; front -= maxsize; while (front < rear) {//刪除front至rear的數(shù)據(jù) element[front++] = 0; if (front == rear) return; } } } } //打印隊列中的元素 template void cirQueue ::print() {//與clearQueue函數(shù)原理一致,將front替換為Index if (!empty()) { int Index = front; while (Index < rear) { cout << element[Index++] << " "; if (Index == rear) { cout << endl; return; } } if (rear < Index) { while (Index <= maxsize - 1) cout << element[Index++] << " "; Index -= maxsize; while (Index < rear) { cout << element[Index++] << " "; if (Index == rear) { cout << endl; return; } } } } } //讀取隊列首個元素 template bool cirQueue ::getTop(T& elem) { if (!empty()) { elem = element[front]; return true; } return false; } template ostream& operator<<(ostream& os, cirQueue & queue) { os << "隊列中的元素數(shù)量為:" << queue.getSize() << endl; return os; } //判斷隊列是否已滿 template bool cirQueue ::_full()const { if (front - rear == 1 || front - rear == -maxsize + 1) return true; return false; } #endif // !CIRCULARQUEUE_H
main.cpp
#include"CircularQueue.h" int main() { cirQueuecq(20); int a = 0; for (int i = 0; i < 19; i++) { cq.push(i); } cq.print(); cout << cq; for (int i = 0; i < 20; i++) { cq.pop(a); } cout << cq;//此時front=rear=19 cout << cq.getfront() << " " << cq.getrear() << endl; //for (int i = 19; i < 25; i++) //{ // cq.push(i); //} cq.push(19); cq.print(); cout << cq.getfront() << " " << cq.getrear() << endl; cout << endl << endl; cq.push(20); cq.getTop(a); cout << a << endl; cq.print(); cout << cq.getfront() << " " << cq.getrear() << endl; return 1; }
關(guān)于“C++怎么實現(xiàn)循環(huán)隊列”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
標題名稱:C++怎么實現(xiàn)循環(huán)隊列
文章網(wǎng)址:http://www.ef60e0e.cn/article/gdjcge.html