循环队列---队列的顺序存储结构

严蔚敏 数据结构(C语言) P64

///循环队列---队列的顺序存储结构
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <malloc.h>
using namespace std;
#define OK 1
#define ERROR -1
#define TRUE 1
#define FALSE 0
#define MAXQSIZE 100
typedef int Status;
typedef int QElemType;

typedef struct
{
    QElemType *base;//初始化的动态分配存储空间
    int Qfront;//头指针,若队列不空,指向队列头元素
    int Qrear;//尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;
//构造一个空队列Q
Status InitQueue(SqQueue &Q);
//销毁队列Q, 队列Q不再存在
Status DestroyQueue(SqQueue &Q);
//把Q置为空队列
Status ClearQueue(SqQueue &Q);
//若Q为空队列,则返回true,否则返回FALSE
Status QueueEmpty(SqQueue Q);
//返回Q的元素个数,即为队列的长度
int QueueLength(SqQueue Q);
/*获取队头元素,
若队列不空,则用e返回Q的队头元素,并返回OK,
否则返回ERROR*/
Status GetHead(SqQueue Q, QElemType &e);
//插入元素e作为新的队列元素
Status EnQueue(SqQueue &Q, QElemType e);
//若队列不空,则删除Q的队首元素,用e返回其值,并返回OK;否则返回ERROR
Status DeQueue(SqQueue &Q, QElemType &e);
//读出元素
Status vi(QElemType e);
//从队头到队尾依次对队中的每个元素调用函数visit(). 一旦visit()失败, 则操作失败
Status QueueTraverse(SqQueue Q, Status(*visit)(QElemType));
int main()
{
    SqQueue Q;
    InitQueue(Q);
    if(QueueEmpty(Q))
    {
        printf("创建队列之后, 队列为空\n");
    }
    for(int i = 0; i < 6; i++)
    {
        EnQueue(Q, i);
    }
    if(!QueueEmpty(Q))
    {
        printf("添加元素之后, 队列不空\n");
    }
    QueueTraverse(Q, vi);//遍历队列中元素
    int length = QueueLength(Q);
    printf("队列的长度: %d\n", length);
    QElemType e;
    GetHead(Q, e);
    printf("队首元素: %d\n", e);
    printf("队尾元素: %d\n", Q.base[Q.Qrear - 1]);
    DeQueue(Q, e);
    printf("删除的队首元素: %d\n", e);
    QueueTraverse(Q, vi);//遍历队列中元素
    ClearQueue(Q);
    if(QueueEmpty(Q))
    {
        printf("清空队列之后, 队列为空\n");
    }
    DestroyQueue(Q);
    return 0;
}
//构造一个空队列Q
Status InitQueue(SqQueue &Q)
{
    Q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
    if(!Q.base)
    {
        printf("存储分配失败!\n");
        return ERROR;
    }
    Q.Qfront = Q.Qrear = 0;
    return OK;
}
//销毁队列Q, 队列Q不再存在
Status DestroyQueue(SqQueue &Q)
{
    Q.base = NULL;
    free(Q.base);
    return OK;
}
//把Q置为空队列
Status ClearQueue(SqQueue &Q)
{
    Q.Qfront = Q.Qrear = 0;
    return OK;
}
//若Q为空队列,则返回true,否则返回FALSE
Status QueueEmpty(SqQueue Q)
{
    if((Q.Qfront == 0) && (Q.Qrear == 0))
    {
        return TRUE;
    }
    return FALSE;
}
//返回Q的元素个数,即为队列的长度
int QueueLength(SqQueue Q)
{
    return (Q.Qrear - Q.Qfront + MAXQSIZE) % MAXQSIZE;
}
/*获取队头元素,
若队列不空,则用e返回Q的队头元素,并返回OK,
否则返回ERROR*/
Status GetHead(SqQueue Q, QElemType &e)
{
    if((Q.Qfront==0) && (Q.Qrear==0))
    {
        return ERROR;
    }
    e = Q.base[Q.Qfront];
    return OK;
}
//插入元素e作为新的队列元素
Status EnQueue(SqQueue &Q, QElemType e)
{
    if((Q.Qrear + 1) % MAXQSIZE == Q.Qfront)
    {
        return ERROR;//队列满
    }
    Q.base[Q.Qrear] = e;
    Q.Qrear = (Q.Qrear + 1) % MAXQSIZE;
    return OK;
}
//若队列不空,则删除Q的队首元素,用e返回其值,并返回OK;否则返回ERROR
Status DeQueue(SqQueue &Q, QElemType &e)
{
    if(Q.Qfront == Q.Qrear)
    {
        return ERROR;
    }
    e = Q.base[Q.Qfront];
    Q.Qfront = (Q.Qfront + 1) % MAXQSIZE;
    return OK;
}
//读出元素
Status vi(QElemType e)
{
    printf("%d ", e);
    return OK;
}
//从队头到队尾依次对队中的每个元素调用函数visit(). 一旦visit()失败, 则操作失败
Status QueueTraverse(SqQueue Q, Status(*visit)(QElemType))
{
    QElemType e;
    printf("遍历队列元素: ");
    int i = Q.Qfront;
    while(i != Q.Qrear)
    {
        e = Q.base[i];
        i++;
        if(!(*visit)(e))
        {
            return ERROR;
        }
    }
    printf("\n");
    return OK;
}

猜你喜欢

转载自blog.csdn.net/weixin_42480264/article/details/81328176