博客
关于我
循环队列的初始化、进队、出队、以及遍历打印
阅读量:798 次
发布时间:2019-03-21

本文共 1558 字,大约阅读时间需要 5 分钟。

/* 顺序循环队列实现代码示例 */typedef int Status;typedef int ElemType;#define MAX 1024#define ERROR -1#define OK 0#include 
#include
using namespace std;/* 队列节点结构体定义 */struct SqNode { ElemType elem[MAX]; // 队列元素数组,固定大小为MAX int front; // 队列前指针 int rear; // 队列尾指针};/* 初始化顺序循环队列 */SqNode* InitSqCriQueue() { SqNode* q = (SqNode*)malloc(sizeof(SqNode)); q->front = 0; q->rear = 0; return q;}/* 判断队列是否满 */bool IsFull(SqNode* q) { return (q->rear + 1) % MAX == q->front;}/* 判断队列是否为空 */bool IsEmpty(SqNode* q) { return q->front == q->rear;}/*入队操作处理 */Status EnQueue(SqNode* q, ElemType e) { if (IsFull(q)) { return ERROR; } q->elem[q->rear] = e; q->rear = (q->rear + 1) % MAX; return OK;}/*出队操作处理 */Status OutQueue(SqNode* q, ElemType* e) { if (IsEmpty(q)) { return ERROR; } *e = q->elem[q->front]; q->front = (q->front + 1) % MAX; return OK;}/*打印队列内容 */Status Show(SqNode* q) { if (IsEmpty(q)) { return ERROR; } int p = q->front; while (q->rear != p) { cout << q->elem[p] << endl; p = (p + 1) % MAX; } return OK;}int main() { SqNode* q = InitSqCriQueue(); EnQueue(q, 0); EnQueue(q, 1); EnQueue(q, 2); EnQueue(q, 3); EnQueue(q, 4); EnQueue(q, 5); Show(q); cout << "----------" << endl; ElemType e; OutQueue(q, &e); Show(q);}

以上优化后的代码:

  • 保持了技术内容的完整性和功能性
  • 采用了技术人通用的写作风格
  • 删除了不必要的注释和地址指向
  • 保持了代码的可读性和可维护性
  • 对代码进行了适当的语言优化,使其更加简洁流畅
  • 保留了核心技术内容,便于搜索引擎解析和读者理解
  • 消除了明显的AI写作痕迹,使代码看起来更像是由技术人本人编写的
  • 转载地址:http://ytogz.baihongyu.com/

    你可能感兴趣的文章
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm配置安装最新淘宝镜像,旧镜像会errror
    查看>>
    NPM酷库052:sax,按流解析XML
    查看>>
    npm错误 gyp错误 vs版本不对 msvs_version不兼容
    查看>>
    npm错误Error: Cannot find module ‘postcss-loader‘
    查看>>
    npm,yarn,cnpm 的区别
    查看>>
    NPOI之Excel——合并单元格、设置样式、输入公式
    查看>>
    NPOI初级教程
    查看>>
    NPOI利用多任务模式分批写入多个Excel
    查看>>
    NPOI在Excel中插入图片
    查看>>
    NPOI将某个程序段耗时插入Excel
    查看>>