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

    你可能感兴趣的文章
    Python SOCKS5代理客户端HTTPS
    查看>>
    Python Soc网络分析:通过使用函数迭代列表来计算机会网络
    查看>>
    Python Sphinx自动摘要:成员函数的自动列表
    查看>>
    Python SQL和NoSQL数据库操作实战
    查看>>
    python stdout flush_sys.stdout.flush()方法的用法
    查看>>
    python string 运算
    查看>>
    Python str与bytes之间的转换
    查看>>
    Python subprocess ffmpeg
    查看>>
    python subprocess Permission denied Errno 13
    查看>>
    Python subprocess.call - 将变量添加到 subprocess.call
    查看>>
    Python Subprocess.Popen 从一个线程
    查看>>
    Python subprocess.Popen 作为 Windows 上的不同用户
    查看>>
    Python subprocess.Popen() 等待完成
    查看>>
    Python sum 二维列表中具有相同第一个值的元素
    查看>>
    Python Sympy模块NoConversion:收敛到根失败;请尝试n<;15或MaxSteps>;50
    查看>>
    python time模块
    查看>>
    Python Tkinter Multiple Windows 教程
    查看>>
    Python tkinter 中的多处理
    查看>>
    Python Tkinter 笔记本小部件
    查看>>
    python try except finally_Python3基础 try-except-finally 的简单示例
    查看>>