C++迴圈順序隊列,迴圈順序隊列

來源:互聯網
上載者:User

C++迴圈順序隊列,迴圈順序隊列

顧名思義:採用順序結構存放的隊列稱為順序隊列
迴圈順序隊列可以避免隊列的假溢出現象的發生。如示,迴圈隊列的幾種特殊情況。

學習完順序迴圈隊列個人感覺應該注意的事項:
front、rear只是表示在base[i]這個順序表中的索引值,而不是記憶體的絕對位址,這樣也才在後面的迴圈的時候處理起來比較方便
隊列迴圈的關鍵

front=(front+1)%queueSize;

下面是個人的迴圈隊列操作工程檔案:

///////////////////////////////////////////////////////////////////////////sqQueue.h ////////////////////////////////////////////////////////////////////////#ifndef MYHEAD_H  #define MYHEAD_H  #include"myhead.h"#endif#include<iomanip>////////////////////////////////////////////////////////////////////////////////迴圈順序隊列資料結構C++類聲明(基類)template <typename ElemType>class SqQueue{public:    void clear();//把迴圈順序隊置空    Status deQueue(ElemType & e);//出隊列    Status enQueue(ElemType & e);//進隊列    Status getFront(ElemType & e);//讀迴圈順序隊列隊頭的元素    int getLength();//求迴圈順序隊中元素個數    bool isEmpty();//判斷迴圈順序隊是否為空白    bool isFull();//判斷迴圈順序隊是否為滿    SqQueue<ElemType> operator =(SqQueue<ElemType> rightQ);//重載賦值運算子的定義    void display();    void randSqueue();    //****************************下面為系統自動調用建構函式及解構函式聲明*************************//    SqQueue(int size=20);//建構函式    ~SqQueue();//解構函式    SqQueue(const SqQueue<ElemType> & otherQ);//拷貝初始化建構函式protected:    int rear;    int front;    int queueSize;    ElemType *base;};/////////////////////////////////////////////////////////////////////////////////////////////////迴圈順序隊列資料結構C++類實現(基類)template <typename ElemType>void SqQueue<ElemType>::clear(){    front=rear;}template <typename ElemType>Status SqQueue<ElemType>::deQueue(ElemType & e){    if(isEmpty())        return ERROR;    e=base[front];    front=(front+1)%queueSize;    return OK;}template <typename ElemType>Status SqQueue<ElemType>::enQueue(ElemType &e){    if(isFull())        return ERROR;    base[rear]=e;    rear=(rear+1)%queueSize;    return OK;}template <typename ElemType>Status SqQueue<ElemType>::getFront(ElemType & e){    if(isEmpty())        return ERROR;    e=base[front]    return OK;}template <typename ElemType>int SqQueue<ElemType>::getLength(){    return (rear-front+queueSize)%queueSize;}template <typename ElemType>bool SqQueue<ElemType>::isEmpty(){    return rear==front?true:false;}template <typename ElemType>bool SqQueue<ElemType>::isFull(){    return (rear+1)%queueSize==front?true:false;}///////系統建構函式及解構函式的實現template <typename ElemType>SqQueue<ElemType>::SqQueue(int size){    base=new ElemType[size];    assert(base!=0);    front=rear=0;    queueSize=size;}template<typename ElemType>SqQueue<ElemType>::~SqQueue(){    delete []base;}template<typename ElemType>SqQueue<ElemType>::SqQueue(const SqQueue<ElemType>& otherQ){    base=new ElemType[otherQ.queueSize];    assert(base!=0);    queueSize=otherQ.queueSize;    front=otherQ.front;    rear=otherQ.rear;    for (int i = front;i%queueSize!=rear)    {        base[i]=otherQ.base[i];        i=(i+1)%queueSize;    }}template<typename ElemType>void SqQueue<ElemType>::display(){    int n=getLength();    cout<<endl;    cout<<"當前的隊列為:"<<endl;    for (int i = 0; i < n; i++)    {        cout<<setw(6)<<base[i+front];    }    cout<<endl;    cout<<setw(6)<<"↑";    for (int i = 0; i < n-1; i++)    {            cout<<setw(6)<<" ";    }    cout<<setw(6)<<"↑"<<endl;    cout<<setw(6)<<"front";    for (int i = 0; i < n-1; i++)    {            cout<<setw(6)<<" ";    }    cout<<setw(6)<<"rear"<<endl;}template<typename ElemType>void SqQueue<ElemType>::randSqueue(){    ElemType Elem[11];    srand(unsigned(time(NULL)));    int n=rand()%10+1;    cout<<"產生的隨機數組是:"<<endl;    for (int i = 0; i < n; i++)    {        Elem[i]=rand()%100+1;        cout<<setw(6)<<Elem[i];    }    for (int i = 0; i < n; i++)    {        base[i]=Elem[i];        rear++;    }    display();}
// SqQueueTest.cpp : 定義控制台應用程式的進入點。//#include "stdafx.h"#include"SqQueue.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){    SqQueue<int> SQ(10);    SQ.randSqueue();    char YesOrNo='Y';    int num,a;    Status sta;    while (YesOrNo=='Y'||YesOrNo=='y')    {        cout<<"1. 進隊列:"<<endl;        cout<<"2. 出隊列:"<<endl;        cin>>num;        switch (num)        {        case 1:            cout<<"輸入進隊列的值:"<<endl;            cin>>a;            sta=SQ.enQueue(a);            if (sta==ERROR)                cout<<"隊列已滿!!!"<<endl;            break;        case 2:            sta=SQ.deQueue(a);            if (sta==ERROR)                cout<<"隊列已空!!!"<<endl;            else                cout<<"彈出的元素為:"<<a<<endl;            break;        default:            break;        }        SQ.display();        cout<<"是否想要繼續?是請輸入Y"<<endl;        cin>>YesOrNo;    }    system("pause");    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.