博客
关于我
循环队列的初始化、进队、出队、以及遍历打印
阅读量: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/

    你可能感兴趣的文章
    Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
    查看>>
    NMAP网络扫描工具的安装与使用
    查看>>
    NMF(非负矩阵分解)
    查看>>
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>