数据结构练习——运算标志来判断队列空满

【题目描述】

如果对循环队列采用设置运算标志的方法来区分队列的满和空的状态,试给出对应的各运算的实现。

【算法描述】

这里需要设置一个标志变量tag,当front=rear,且tag=0时队列为队列空,当front=rear,且tag=1时为队列满!

代码实现如下:

#include <iostream>
using namespace std;

#define MAXSIZE 6
typedef int QElemType;
typedef struct
{
    QElemType data[MAXSIZE];
    int front;
    int rear;
    int tag;                        //设置标志符tag来判断队列状态
} SqQueue;

//初始化
void InitQueue(SqQueue *Q)
{
    Q->front = Q->rear = 0;
    Q->tag = 0;                     //0为未满,1为满队
}

//若队列Q为空队列,则返回TRUE,否则返回FALSE 
bool QueueEmpty(SqQueue *Q)
{
    if (Q->front == Q->rear && Q->tag == 0)      //队列空的标志 
        return true;                                
    else
        return false;
}

//若队列Q为满队列,则返回TRUE,否则返回FALSE 
bool QueueFull(SqQueue *Q)
{
    if (Q->front == Q->rear && Q->tag == 1)      //队列满的标志(可简化为tag==1) 
        return true;
    else
        return false;
}

//返回队列的当前长度 
int QueueLength(SqQueue* Q)
{
    if (Q->tag == 1)                //队满的情况
    {
        return MAXSIZE;
    }
    return  (Q->rear - Q->front + MAXSIZE) % MAXSIZE;    //返回队列长度
}

//获取队头元素
bool GetHead(SqQueue *Q, QElemType* e)
{
    if (QueueEmpty(Q))                       //队列空 
        return false;
    *e = Q->data[Q->front];                  //用e返回队列头元素
    return true;                                
}

//进队列
bool Enqueue(SqQueue *Q, QElemType e)
{
    if (Q->tag == 1)                         //队列满
    {
        cout << "队列已满,进队失败!!!" <<endl;
        return false;
    }
    Q->data[Q->rear] = e;                    //将元素e赋给队尾   
    Q->rear = (Q->rear + 1) % MAXSIZE;       //尾指针后移一个位置
    if (Q->front == Q->rear)
    {
        Q->tag = 1;                          //判断进队后队列是否满队
    }
    return true;
}
//出队列
bool Dequeue(SqQueue *Q, QElemType *e)
{
    if (QueueEmpty(Q))                       //队列空
    {
        cout << "队列为空,出队失败!!!" <<endl;
        return false;
    }
    *e = Q->data[Q->front];                  //删除队列中对头元素并返回给e
    Q->front = (Q->front + 1) % MAXSIZE;     //头指针向后移动一个位置
    Q->tag = 0;                              //删除后队列必然不为满队列,故统一将tag设为0
    return true;
}

// 从队头到队尾依次输出每个元素
void QueueTraverse(SqQueue *Q)
{
    if (QueueEmpty(Q))
    {
        cout << "队列为空 !!!" << endl;
    }
    else
    {
        int i = Q->front;
        cout << "队头到队尾元素依次是 : " ;
        //队列未满时的输出情况
        if (Q->front != Q->rear && Q->tag == 0)
        {
            while (i != Q->rear)
            {
                cout << Q->data[i] << " ";
                i = (i + 1) % MAXSIZE;
            }
        }
        //满队列时的输出情况
        //关于满队列时的输出情况,博主暂时只想到这种输出方法,欢迎大家来补充交流
        else                                    
        {
            for (int i = 0; i < MAXSIZE; i++)
            {
                cout << Q->data[(Q->front+i)%MAXSIZE] << " ";
            }
        }
        cout << endl;
    }
}

int main()
{
    SqQueue *Q;
    Q = new SqQueue;
    InitQueue(Q);
    for (int i = 0; i < 5; i++)
    {
        Enqueue(Q, i);
    }
    cout << "队列长度是 : " << QueueLength(Q) << endl;
    int e;
    GetHead(Q, &e);
    cout << "队头元素是 : " << e << endl;
    QueueTraverse(Q);
    int a, b, c;
    Dequeue(Q, &a);
    Dequeue(Q, &b);
    Dequeue(Q, &c);
    cout << "-----------------------------------" <<endl;
    cout << "删除三个元素后 !" << endl;
    cout << "删除的元素是 : " << a << " " << b << " " << c << endl;
    QueueTraverse(Q);

    return 0;
}

【实现效果】

【补充说明】

博主在这里只是建立了一个顺序队列,同样也可以选择建立一个链队列,实现的思想是差不多的,有兴趣的小伙伴也可以去自己尝试一下,博主为了完成作业所以选择了相对简单的顺序队列】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值