欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實現(xiàn)循環(huán)隊列

 更新時間:2020年01月13日 11:42:19   作者:My_Algorithm  
這篇文章主要為大家詳細介紹了C++實現(xiàn)循環(huán)隊列,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++實現(xiàn)循環(huán)隊列的具體代碼,供大家參考,具體內(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;
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C++11的functional模塊介紹和使用案例

    C++11的functional模塊介紹和使用案例

    functional模塊是C++ 11提供了一組函數(shù)對象和算法,用于增強C++的函數(shù)式編程能力,本文主要介紹了C++11的functional模塊介紹和使用案例,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • opengl繪制五星紅旗

    opengl繪制五星紅旗

    這篇文章主要為大家詳細介紹了opengl繪制五星紅旗的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • c++實現(xiàn)MD5算法實現(xiàn)代碼

    c++實現(xiàn)MD5算法實現(xiàn)代碼

    用c++實現(xiàn)了md5算法。包含 md5.h 和md5.cpp 兩個文件。主要參考百度百科 “MD5” 原理,代碼中變量命名也是參考其中的公式,程序的使用說明在md5.h 文件的末尾注釋中
    2013-11-11
  • C語言中進制知識匯總

    C語言中進制知識匯總

    在C語言里,整數(shù)有三種表示形式:十進制,八進制,十六進制。 其中以數(shù)字0開頭,由0~7組成的數(shù)是八進制。以0X或0x開頭,由0~9,A~F或a~f 組成是十六進制。除表示正負的符號外,以1~9開頭,由0~9組成是十進制。
    2016-05-05
  • 基于Protobuf C++ serialize到char*的實現(xiàn)方法分析

    基于Protobuf C++ serialize到char*的實現(xiàn)方法分析

    本篇文章是對Protobuf C++ serialize到char*的實現(xiàn)方法進行了詳細的分析介紹。需要的朋友參考下
    2013-05-05
  • C語言數(shù)據(jù)結(jié)構(gòu)與算法之鏈表(一)

    C語言數(shù)據(jù)結(jié)構(gòu)與算法之鏈表(一)

    鏈表是線性表的鏈式存儲方式。鏈表的內(nèi)存是不連續(xù)的,前一個元素存儲地址的下一個地址中存儲的不一定是下一個元素。小編今天就將帶大家深入了解一下鏈表,快來學習吧
    2021-12-12
  • C語言實現(xiàn)共享單車管理系統(tǒng)

    C語言實現(xiàn)共享單車管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)共享單車管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • C語言實現(xiàn)圖的鄰接矩陣存儲操作

    C語言實現(xiàn)圖的鄰接矩陣存儲操作

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)圖的鄰接矩陣存儲操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • c++ 構(gòu)造函數(shù)中調(diào)用虛函數(shù)的實現(xiàn)方法

    c++ 構(gòu)造函數(shù)中調(diào)用虛函數(shù)的實現(xiàn)方法

    下面小編就為大家?guī)硪黄猚++ 構(gòu)造函數(shù)中調(diào)用虛函數(shù)的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • 深入解析C++中的mutable關鍵字

    深入解析C++中的mutable關鍵字

    在C++中,mutable也是為了突破const的限制而設置的。被mutable修飾的變量,將永遠處于可變的狀態(tài),即使在一個const函數(shù)中
    2013-10-10

最新評論