循环队列的C++实现

仅供参考

#include <iostream>
using namespace std;

// 函数结果状态代码
#define TRUE    1
#define FALSE   0
#define OK      1
#define ERROR   0
#define INFEASIBLE -1
#define OVERFLOW   -2
#define MAXSIZE    100

// Status 是函数的类型 其值是函数结果状态代码
typedef int Status;

typedef int QElemType;

typedef struct SqQueue{

    QElemType *base;    // 初始化的动态分配存储空间
    int Front;          // 头指针
    int Rear;           // 尾指针
}SqQueue;

// 构造空队列
Status InitQueue(SqQueue &Q);

// 销毁队列
Status DestroyQueue(SqQueue &Q);

// 清空队列
Status ClearQueue(SqQueue &Q);

// 求队列长度
int QueueLength(SqQueue Q);

// 用e返回Q的队头元素
Status GetHead(SqQueue Q, QElemType &e);

// 插入元素e为Q的队尾元素
Status EnQueue(SqQueue &Q, QElemType e);

// 删除Q的队头元素,用e返回
Status DeQueue(SqQueue &Q, QElemType &e);

// 遍历队列
Status TraverseQueue(SqQueue Q);

int main() {

    SqQueue Q;
    InitQueue(Q);
    EnQueue(Q, 11);
    EnQueue(Q, 22);
    EnQueue(Q, 33);
    int length = QueueLength(Q);
    cout << "队列长度: " << length << endl;
    TraverseQueue(Q);
    QElemType e;
    GetHead(Q,e);
    cout << "队头元素: " << e << endl;
    DeQueue(Q, e);
    cout << "删除队头元素: "  << e << endl;
    TraverseQueue(Q);
    ClearQueue(Q);
    length = QueueLength(Q);
    cout << "队列长度: " << length << endl;
    DestroyQueue(Q);
    return 0;
}

// 构造空队列
Status InitQueue(SqQueue &Q){

    Q.base = new QElemType[MAXSIZE];    // 分配数组空间
    if (!Q.base) exit(OVERFLOW);
    Q.Front = 0;                        // 头指针 尾指针为0, 队列为空
    Q.Rear = 0;
    return OK;
}

// 销毁队列
Status DestroyQueue(SqQueue &Q){
    if (!Q.base) return ERROR;
    delete Q.base;
    Q.base = nullptr;
    cout << "队列已销毁" << endl;
    return OK;
}

// 清空队列
Status ClearQueue(SqQueue &Q){
    if (Q.Front == Q.Rear) return ERROR;    // 已经是空表
    Q.Rear = Q.Front;
    cout << "队列已清空" << endl;
    return OK;
}

// 求队列长度
int QueueLength(SqQueue Q){

    return ((Q.Rear-Q.Front+MAXSIZE)%MAXSIZE);
}

// 用e返回Q的队头元素
Status GetHead(SqQueue Q, QElemType &e){
    if (Q.Front == Q.Rear)
        return ERROR;           // 队不能为空
    e = Q.base[Q.Front];        // 返回队头指针元素, 队头指针不变
    return OK;
}

// 插入元素e为Q的队尾元素(入队)
Status EnQueue(SqQueue &Q, QElemType e){

    if ((Q.Rear+1)%MAXSIZE == Q.Front) return ERROR;    // 队满
    Q.base[Q.Rear] = e;         // 新元素加入队尾
    Q.Rear = (Q.Rear+1)%MAXSIZE;    // 队尾指针+1
    return OK;
}

// 删除Q的队头元素,用e返回(出队)
Status DeQueue(SqQueue &Q, QElemType &e){

    if (Q.Rear == Q.Front) return ERROR; // 队空
    e = Q.base[Q.Front];                // 保存队头元素
    Q.Front = (Q.Front+1)%MAXSIZE;  // 队头指针+1
    return OK;
}

// 遍历队列
Status TraverseQueue(SqQueue Q){

    if (Q.Front == Q.Rear) return ERROR;
    cout << "遍历队列: ";
    int temp = Q.Front;
    while (temp < Q.Rear){
        cout << Q.base[temp] << " ";
        temp++;
    }
    cout << endl;
    return OK;
}

测试:

队列长度: 3
遍历队列: 11 22 33 
队头元素: 11
删除队头元素: 11
遍历队列: 22 33 
队列已清空
队列长度: 0
队列已销毁

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值