最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談C語言中的sizeof()和strlen()的區(qū)別

    淺談C語言中的sizeof()和strlen()的區(qū)別

    本文主要介紹了C語言中的sizeof()和strlen()的區(qū)別,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • C++ deque容器的用法詳解

    C++ deque容器的用法詳解

    在處理一些數(shù)組的事情,所以隨手保留一下Deque容器的使用方法很有必要,接下來通過本文給大家重點介紹C++ deque容器的用法及deque和vector的區(qū)別講解,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • 關(guān)于PCL出現(xiàn)"無法找到?pcl_commond.dll?文件程序無法執(zhí)行"的問題及解決方法

    關(guān)于PCL出現(xiàn)"無法找到?pcl_commond.dll?文件程序無法執(zhí)行"的問題及解決方法

    這篇文章主要介紹了PCL出現(xiàn)"無法找到?pcl_commond.dll?文件程序無法執(zhí)行"的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • C++實現(xiàn)LeetCode(95.獨一無二的二叉搜索樹之二)

    C++實現(xiàn)LeetCode(95.獨一無二的二叉搜索樹之二)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(95.獨一無二的二叉搜索樹之二),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語言庫的封裝和使用方法總結(jié)

    C語言庫的封裝和使用方法總結(jié)

    在編程的過程中,使用已經(jīng)封裝好的庫函數(shù)是十分方便的,也是十分高效的,這篇文章主要給大家介紹了關(guān)于C語言庫的封裝和使用的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • C++中4種類型轉(zhuǎn)換方式 cast操作詳解

    C++中4種類型轉(zhuǎn)換方式 cast操作詳解

    static_cast,支持子類指針到父類指針的轉(zhuǎn)換,并根據(jù)實際情況調(diào)整指針的值,反過來也支持,但會給出編譯警告,它作用最類似C風(fēng)格的“強制轉(zhuǎn)換”,一般來說可認為它是安全的
    2013-10-10
  • C/C++中虛函數(shù)詳解及其作用介紹

    C/C++中虛函數(shù)詳解及其作用介紹

    這篇文章主要介紹了C/C++中虛函數(shù)詳解及其作用介紹,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • C語言實現(xiàn)單鏈表的快速排序算法

    C語言實現(xiàn)單鏈表的快速排序算法

    大家好,本篇文章主要講的是C語言實現(xiàn)單鏈表的快速排序算法,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • C利用語言實現(xiàn)數(shù)據(jù)結(jié)構(gòu)之隊列

    C利用語言實現(xiàn)數(shù)據(jù)結(jié)構(gòu)之隊列

    隊列 (Queue):簡稱隊,是另一種限定性的線性表,它只允許在表的一端插入元素,而在另一端刪除元素。q=(a1, a2, a3, … an),其中a1為隊頭,an為隊尾,下面文章小編將為大家詳細介紹,需要的下伙伴可以參考一下
    2021-10-10
  • VS?Code安裝及C、C++環(huán)境配置詳細教程(Windows系統(tǒng))

    VS?Code安裝及C、C++環(huán)境配置詳細教程(Windows系統(tǒng))

    這篇文章主要介紹了VS?Code安裝及C、C++環(huán)境配置詳細教程(Windows系統(tǒng)),本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-02-02

最新評論

宝兴县| 绥宁县| 新丰县| 扶余县| 景谷| 桃园市| 永和县| 贵港市| 新巴尔虎左旗| 化德县| 清苑县| 博兴县| 万山特区| 丽江市| 安国市| 太仆寺旗| 封开县| 乌拉特前旗| 耒阳市| 米林县| 西吉县| 化德县| 娄烦县| 石嘴山市| 辽源市| 五大连池市| 祁东县| 城口县| 广南县| 滕州市| 赤壁市| 靖江市| 马尔康县| 江油市| 河南省| 黎城县| 峨边| 亳州市| 奉贤区| 禹州市| 鹤岗市|