欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C++中怎么實現(xiàn)一個循環(huán)隊列-創(chuàng)新互聯(lián)

本篇文章給大家分享的是有關(guān)C++中怎么實現(xiàn)一個循環(huán)隊列,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

創(chuàng)新互聯(lián)公司主營富寧網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,App定制開發(fā),富寧h5微信小程序定制開發(fā)搭建,富寧網(wǎng)站營銷推廣歡迎富寧等地區(qū)企業(yè)咨詢

具體內(nèi)容如下

circularQueue.h

#pragma once#pragma once#ifndef CIRCULARQUEUE_H#define CIRCULARQUEUE_H #include<iostream>#include<ostream>using std::cout;using std::cin;using std::endl;using std::ostream;template<class T> class cirQueue; template<typename T>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<typename T> friend ostream& operator<<(ostream& os, cirQueue<T>& queue); private: bool _full()const;//判斷隊列是否已滿 int maxsize;//隊列較大的空間 T* element;//存放于隊列中的元素數(shù)組 int front;//模擬隊頭指針 int rear;//模擬隊尾指針};  template<typename T>cirQueue<T>::cirQueue(int sz) { maxsize = sz; element = new T[maxsize]; if (element == nullptr) cout << "內(nèi)存分配失敗" << endl; front = 0; rear = 0;}  template<typename T>cirQueue<T>::~cirQueue() { if (element != nullptr) delete element;} //進隊template<typename T>void cirQueue<T>::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<typename T>void cirQueue<T>::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<typename T>bool cirQueue<T>::empty() { if (front == rear)//待定 return true; return false;} //返回隊列中元素的個數(shù)template<typename T>int cirQueue<T>::getSize() { int num = 0; if (front <= rear) return rear - front; else return maxsize - front + rear + 1;} //清空隊列中的元素template<typename T>void cirQueue<T>::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<typename T>void cirQueue<T>::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<typename T>bool cirQueue<T>::getTop(T& elem) { if (!empty()) { elem = element[front]; return true; } return false;} template<typename T>ostream& operator<<(ostream& os, cirQueue<T>& queue) { os << "隊列中的元素數(shù)量為:" << queue.getSize() << endl; return os;} //判斷隊列是否已滿template<typename T>bool cirQueue<T>::_full()const { if (front - rear == 1 || front - rear == -maxsize + 1) return true; return false;} #endif // !CIRCULARQUEUE_H

main.cpp

#include"CircularQueue.h"  int main(){ cirQueue<int> cq(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;}

以上就是C++中怎么實現(xiàn)一個循環(huán)隊列,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章名稱:C++中怎么實現(xiàn)一個循環(huán)隊列-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://chinadenli.net/article4/cohoie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作外貿(mào)網(wǎng)站建設(shè)軟件開發(fā)用戶體驗建站公司服務器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司