当前位置: 首页 > news >正文

队列的数组实现

声明 curosr_queue.h:

 1 #ifndef CURSOR_QUEUE_H_INCLUDED
 2 #define CURSOR_QUEUE_H_INCLUDED
 3 struct QueueRecord;
 4 typedef struct QueueRecord *Queue;
 5 
 6 int IsEmpty(Queue Q);
 7 int IsFull(Queue Q);
 8 Queue CreateQueue(int MaxElements);
 9 void DisposeQueue(Queue Q);
10 void MakeEmpty(Queue Q);
11 void Enqueue(int X, Queue Q);
12 int Front(Queue Q);
13 int Dequeue(Queue Q);
14 int FrontAndDequeue(Queue Q);
15 
16 #endif // CURSOR_QUEUE_H_INCLUDED

实现 implementation.c:

 1 #include<stdio.h>
 2 #include "cursor_queue.h"
 3 
 4 #define MinQueueSize 5
 5 
 6 struct QueueRecord{
 7     int Capacity;
 8     int Front;
 9     int Rear;
10     int Size;
11     int *Array;
12 };
13 
14 int IsEmpty(Queue Q){
15     return Q->Size == 0;
16 }
17 
18 Queue CreateQueue(int MaxElements) {
19     Queue Q;
20     if(MaxElements < MinQueueSize)
21         printf("Too small queue size!");
22     Q = malloc(sizeof(struct QueueRecord));
23     if(Q == NULL) {
24         printf("Out of space!");
25     }
26     Q->Array = malloc(sizeof(int) * MaxElements);
27     if(Q->Array == NULL){
28         printf("Out of space!");
29     }
30     MakeEmpty(Q);
31     Q->Capacity = MaxElements;
32     return Q;
33 }
34 
35 void MakeEmpty(Queue Q) {
36     Q->Rear = 0;
37     Q->Front = 1;
38     Q->Size = 0;
39 }
40 
41 static int Succ(int Value, Queue Q) {
42     if(++Value == Q->Capacity)
43         Value = 0;
44     return Value;
45 }
46 
47 int IsFull(Queue Q) {
48     return Q->Size == Q->Capacity;
49 }
50 
51 void Enqueue(int X, Queue Q) {
52     if(IsFull(Q))
53         printf("Error! Full Queue!\n");
54     else{
55         Q->Size++;
56         Q->Rear = Succ(Q->Rear, Q);
57         Q->Array[Q->Rear] = X;
58     }
59 }
60 
61 int Dequeue(Queue Q){
62     int Data;
63     if(IsEmpty(Q)){
64         printf("Error, empty queue!");
65         return -1;
66     }
67     else{
68         Q->Size--;
69         Data = Q->Array[Q->Front];
70         Q->Front = Succ(Q->Front, Q);
71     }
72     return Data;
73 }
74 
75 void DisposeQueue(Queue Q) {
76     free(Q->Array);
77     free(Q);
78 }

测试 main.c:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "cursor_queue.h"
 4 
 5 int main()
 6 {
 7     Queue Q;
 8     Q = CreateQueue(10);
 9     int i = 0;
10     for(i = 0; i < 10; i++)
11         Enqueue(i ,Q);
12     Enqueue(10, Q);
13     for(i = 0; i < 10; i++)
14         printf("%d ", Dequeue(Q));
15     printf("\n");
16     DisposeQueue(Q);
17     Q = CreateQueue(10);
18     for(i = 0; i < 10; i++)
19         printf("%d\n", Dequeue(Q));
20     return 0;
21 }

 

转载于:https://www.cnblogs.com/zsdvvb/p/4832090.html

相关文章:

  • mac下视频录制及gif图创建
  • JavaScript的DOM编程--07--节点的属性
  • tar解压问题gzip: stdin: not in gzip format
  • iOS开发引入第三方类库的问题
  • IOS 线程 +并发
  • MySQL的自增主键
  • golang echo livereload
  • XML DTD学习
  • 又是一番风味
  • 配置 ASP.NET Linux( CentOS 6.5 ) 运行环境 MONO + Jexus
  • 读书笔记 - 《黑天鹅》
  • bootstrap-scrollspy
  • 著名博客
  • 弹出和收起软键盘
  • [译]Dynamics AX 2012 R2 BI系列-分析的架构
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • classpath对获取配置文件的影响
  • go append函数以及写入
  • JavaScript学习总结——原型
  • Laravel Telescope:优雅的应用调试工具
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • React+TypeScript入门
  • Spark学习笔记之相关记录
  • 翻译:Hystrix - How To Use
  • 仿天猫超市收藏抛物线动画工具库
  • 如何合理的规划jvm性能调优
  • 小程序开发中的那些坑
  • 用jquery写贪吃蛇
  • 用mpvue开发微信小程序
  • 湖北分布式智能数据采集方法有哪些?
  • 组复制官方翻译九、Group Replication Technical Details
  • ​​​​​​​​​​​​​​Γ函数
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • #vue3 实现前端下载excel文件模板功能
  • (03)光刻——半导体电路的绘制
  • (BFS)hdoj2377-Bus Pass
  • (function(){})()的分步解析
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (rabbitmq的高级特性)消息可靠性
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (七)c52学习之旅-中断
  • (三)终结任务
  • (转)树状数组
  • .FileZilla的使用和主动模式被动模式介绍
  • .NET Standard、.NET Framework 、.NET Core三者的关系与区别?
  • .net 获取url的方法
  • .Net语言中的StringBuilder:入门到精通
  • :如何用SQL脚本保存存储过程返回的结果集
  • @Resource和@Autowired的区别
  • [C++]priority_queue的介绍及模拟实现
  • [Hive] CTE 通用表达式 WITH关键字
  • [IE编程] IE中对网页进行截图的编程接口
  • [iOS]随机生成UUID通用唯一识别码