循环队列--忘记分配空间和如何用tag判断队空队满
#include<iostream> #define maxsize 100 using namespace std; struct CLqueue { int *Q; int front; int rear; int tag; }; typedef struct CLqueue *CL; void Init(CL &q) { q = new CLqueue; q->Q=new int[maxsize]; q->tag = 0;//队空 q->rear = q->front=0; } bool Judge(CL &q) { if (q->tag) return true; else return false; } void Enqueue(CL &q,int a) { if (Judge(q)) return; q->Q[q->rear] = a; q->rear = (q->rear +1 ) % maxsize; if ((q->rear + 1) % maxsize == q->front) q->tag = 1; } void Dlqueue(CL &q) { if (!Judge(q)) return; q->front = (q->front + 1) % maxsize; if ((q->front + 1) % maxsize == q->rear) q->tag = 0; } int main() { int n; CL q; while (cin >> n && n != 0) { Init(q); for (int i= 0; i < n; i++) { int a; cin >> a; Enqueue(q, a); } for (int i = q->front; i < q->rear-1; i++) { cout << q->Q[i] << " "; } cout<< q->Q[q->rear-1] << endl; } return 0; }
#include<iostream> #define maxsize 100 using namespace std; struct CLqueue { int *Q; int front; int rear; int tag; }; typedef struct CLqueue *CL; void Init(CL &q) { q = new CLqueue; q->Q=new int[maxsize]; q->tag = 0;//队空 q->rear = q->front=0; } bool Judge(CL &q) { if (q->tag) return true; else return false; } void Enqueue(CL &q,int a) { if (Judge(q)) return; q->Q[q->rear] = a; q->rear = (q->rear +1 ) % maxsize; if ((q->rear + 1) % maxsize == q->front) q->tag = 1; } void Dlqueue(CL &q) { if (!Judge(q)) return; q->front = (q->front + 1) % maxsize; if ((q->front + 1) % maxsize == q->rear) q->tag = 0; } int main() { int n; CL q; while (cin >> n && n != 0) { Init(q); for (int i= 0; i < n; i++) { int a; cin >> a; Enqueue(q, a); } for (int i = q->front; i < q->rear-1; i++) { cout << q->Q[i] << " "; } cout<< q->Q[q->rear-1] << endl; } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码