C++怎么實(shí)現(xiàn)循環(huán)隊(duì)列-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)C++怎么實(shí)現(xiàn)循環(huán)隊(duì)列,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)頁(yè)空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、湖南網(wǎng)站維護(hù)、網(wǎng)站推廣。

具體內(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;
}

關(guān)于“C++怎么實(shí)現(xiàn)循環(huán)隊(duì)列”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

標(biāo)題名稱(chēng):C++怎么實(shí)現(xiàn)循環(huán)隊(duì)列-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://muchs.cn/article44/csgjee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、標(biāo)簽優(yōu)化、全網(wǎng)營(yíng)銷(xiāo)推廣Google、小程序開(kāi)發(fā)、定制網(wǎng)站

廣告

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

手機(jī)網(wǎng)站建設(shè)