溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

C++中怎么實(shí)現(xiàn)一個(gè)循環(huán)隊(duì)列

發(fā)布時(shí)間:2021-07-06 17:36:03 來(lái)源:億速云 閱讀:195 作者:Leah 欄目:編程語(yǔ)言

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

具體內(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);//進(jìn)隊(duì) void pop(T& elem);//出隊(duì) bool empty();//查看隊(duì)列是否為空 int getSize();//返回隊(duì)列中元素的個(gè)數(shù) void clearQueue();//清空隊(duì)列中的元素 void print();//打印隊(duì)列中的元素 int getfront() { return front; } int getrear() { return rear; } bool getTop(T& elem);//讀取隊(duì)列首個(gè)元素  template<typename T> friend ostream& operator<<(ostream& os, cirQueue<T>& queue); private: bool _full()const;//判斷隊(duì)列是否已滿(mǎn) int maxsize;//隊(duì)列最大的空間 T* element;//存放于隊(duì)列中的元素?cái)?shù)組 int front;//模擬隊(duì)頭指針 int rear;//模擬隊(duì)尾指針};  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;} //進(jìn)隊(duì)template<typename T>void cirQueue<T>::push(const T& elem) {//需要保證隊(duì)尾指針位置與首個(gè)元素相差一個(gè)位置 if (rear > (maxsize - 1)) rear -= maxsize ; if (front > (maxsize - 1)) front -= maxsize ; if (!_full()) {//隊(duì)列未滿(mǎn)的情況  element[rear++] = elem;//隊(duì)尾向后移動(dòng)一位 //++rear; } else { cout << "隊(duì)列已滿(mǎn),不能插入!" << endl; return; }} //出隊(duì)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()) {//隊(duì)列未空的情況 elem = element[front++];//隊(duì)頭向后移動(dòng)一位 element[front - 1] = 0;//置零 } else { cout << "隊(duì)列已空!" << endl; return; }} //查看隊(duì)列是否為空template<typename T>bool cirQueue<T>::empty() { if (front == rear)//待定 return true; return false;} //返回隊(duì)列中元素的個(gè)數(shù)template<typename T>int cirQueue<T>::getSize() { int num = 0; if (front <= rear) return rear - front; else return maxsize - front + rear + 1;} //清空隊(duì)列中的元素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;  } } }} //打印隊(duì)列中的元素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;  }  } } }} //讀取隊(duì)列首個(gè)元素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 << "隊(duì)列中的元素?cái)?shù)量為:" << queue.getSize() << endl; return os;} //判斷隊(duì)列是否已滿(mǎn)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;//此時(shí)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++中怎么實(shí)現(xiàn)一個(gè)循環(huán)隊(duì)列,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI