码迷,mamicode.com
首页 > 编程语言 > 详细

C++循环顺序队列

时间:2015-08-27 15:30:55      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:队列   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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

C++循环顺序队列

标签:队列   c++   

原文地址:http://blog.csdn.net/handsomewangjg/article/details/48026185

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!