循环队列(数组实现)

 

 

 

复制代码
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define false 0
 5 #define true 1
 6 
 7 typedef int ElementType;
 8 typedef int Position;
 9 typedef int bool;
10 typedef struct QNode *PtrToQNode;
11 struct QNode
12 {
13    ElementType *Data;
14    Position Front,Rear;
15    int MaxSize;
16 };
17 typedef PtrToQNode Queue;
18 
19 Queue CreateQueue(int MaxSize);   //创建一个最大长度为MaxSize的空队列
20 bool IsFull(Queue Q);   //判断队列是否已满
21 bool IsEmpty(Queue Q);  //判断队列是否为空
22 bool AddQ(Queue Q, ElementType X);    //入队
23 ElementType DeleteQ(Queue Q);         //出队
24 
25 
26 
27 Queue CreateQueue(int MaxSize)
28 {
29     Queue Q = (Queue)malloc(sizeof(struct QNode));
30     Q->Data = (ElementType *)malloc(MaxSize*sizeof(ElementType));
31     Q->Front = Q->Rear = 0;
32     Q->MaxSize = MaxSize;
33     return Q;
34 }
35 
36 bool IsFull(Queue Q)
37 {
38     return ((Q->Rear+1) % (Q->MaxSize)) == Q->Front;
39 }
40 
41 bool IsEmpty(Queue Q)
42 {
43     return Q->Front == Q->Rear;
44 }
45 
46 bool AddQ(Queue Q, ElementType X)
47 {
48     if(IsFull(Q))
49     {
50         printf("队列已满,无法入队!\n");
51         return false;
52     }
53     Q->Rear = (Q->Rear+1)%(Q->MaxSize);
54     Q->Data[Q->Rear] = X;
55     return true;
56 }
57 
58 ElementType DeleteQ(Queue Q)
59 {
60     if(IsEmpty(Q))
61     {
62         printf("队列为空,无法出队!\n");
63         return false;
64     }
65     Q->Front = (Q->Front+1)%(Q->MaxSize);
66     return Q->Data[Q->Front];
67 }
复制代码

 

posted @   拾月凄辰  阅读(234)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示
主题色彩