1. /* queue.h */
  2.  
  3. #ifndef _QUEUE_H
  4. #define _QUEUE_H
  5.  
  6. struct queue_record;
  7. typedef struct queue_record *queue;
  8.  
  9. int is_empty( queue q );
  10. int is_full( queue q );
  11. queue create_queue( int max_elements );
  12. void dispose_queue( queue q );
  13. void make_empty( queue q );
  14. void enqueue( int x, queue q );
  15. int front( queue q );
  16. void dequeue( queue q );
  17. int front_and_dequeue( queue q );
  18.  
  19. #endif
  1. /* queue.c */
  2.  
  3. #include "queue.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define MIN_QUEUE_SIZE 5
  8.  
  9. struct queue_record
  10. {
  11. int capacity;
  12. int front;
  13. int rear;
  14. int size;
  15. int *array;
  16. };
  17.  
  18. void
  19. make_empty( queue q )
  20. {
  21. q->size = 0;
  22. q->front = 1;
  23. q->rear = 0;
  24. }
  25.  
  26. int
  27. is_empty( queue q )
  28. {
  29. return q->size == 0;
  30. }
  31.  
  32. int
  33. is_full( queue q )
  34. {
  35. return q->size == q->capacity;
  36. }
  37.  
  38. queue
  39. create_queue( int max_elements )
  40. {
  41. queue q;
  42.  
  43. if( max_elements < MIN_QUEUE_SIZE )
  44. {
  45. printf("Queue size is too small!\n");
  46. exit(0);
  47. }
  48.  
  49. q = malloc( sizeof(struct queue_record) );
  50. if(q == NULL)
  51. {
  52. printf("Out of space!\n");
  53. exit(1);
  54. }
  55. q->array = malloc(sizeof(int) * max_elements);
  56. if(q->array == NULL)
  57. {
  58. printf("Out of space!\n");
  59. exit(1);
  60. }
  61. q->capacity = max_elements;
  62.  
  63. make_empty(q);
  64.  
  65. return q;
  66. }
  67.  
  68. static int
  69. succ( int value, queue q )
  70. {
  71. if( ++value == q->capacity )
  72. value = 0;
  73. return value;
  74. }
  75.  
  76. void
  77. enqueue( int x, queue q )
  78. {
  79. if( is_full( q ) )
  80. {
  81. printf("full queue!\n");
  82. exit(0);
  83. }
  84. else
  85. {
  86. q->size++;
  87. q->rear = succ( q->rear, q );
  88. q->array[q->rear] = x;
  89. }
  90. }
  91.  
  92. void
  93. dequeue( queue q )
  94. {
  95. if( is_empty( q ) )
  96. {
  97. printf("empty queue!\n");
  98. exit(0);
  99. }
  100. else
  101. {
  102. q->size--;
  103. q->front = succ( q->front, q );
  104. }
  105. }
  106.  
  107. int
  108. front( queue q )
  109. {
  110.  
  111. if( is_empty( q ) )
  112. {
  113. printf("empty queue!\n");
  114. exit(0);
  115. }
  116. else
  117. return q->array[q->front];
  118. }
  119.  
  120. int
  121. front_and_dequeue( queue q )
  122. {
  123. int tmp;
  124.  
  125. if( is_empty( q ) )
  126. {
  127. printf("empty queue!\n");
  128. exit(0);
  129. }
  130. else
  131. {
  132. tmp = q->array[q->front];
  133. q->size--;
  134. q->front = succ( q->front, q );
  135. return tmp;
  136. }
  137.  
  138. }
  139.  
  140. void
  141. dispose_queue( queue q )
  142. {
  143. if( q != NULL )
  144. {
  145. free(q->array);
  146. free(q);
  147. }
  148. }
  1. /* queue_test.c */
  2.  
  3. #include "queue.h"
  4. #include <stdio.h>
  5.  
  6. int
  7. main(void)
  8. {
  9. queue q;
  10. int tmp;
  11. int i;
  12.  
  13. q = create_queue(10);
  14. printf("1 enqueue\n");
  15. enqueue(1, q);
  16. printf("2 enqueue\n");
  17. enqueue(2, q);
  18. printf("3 enqueue\n");
  19. enqueue(3, q);
  20. printf("4 enqueue\n");
  21. enqueue(4, q);
  22. printf("5 enqueue\n");
  23. enqueue(5, q);
  24.  
  25. printf("\n");
  26. for(i=0; i<5; i++)
  27. {
  28. printf("dequeue %d\n", front_and_dequeue( q ));
  29.  
  30. }
  31.  
  32. }

测试结果:

队列实例程序(C语言)的更多相关文章

  1. tesseract ocr文字识别Android实例程序和训练工具全部源代码

    tesseract ocr是一个开源的文字识别引擎,Android系统中也可以使用.可以识别50多种语言,通过自己训练识别库的方式,可以大大提高识别的准确率. 为了节省大家的学习时间,现将自己近期的学 ...

  2. Delphi XE5教程3:实例程序

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  3. MSMQ-发送消息到远程专用队列 实例

    目录 一:MSMQ的一些理论上的知识 二:队列类型(Queue Type) 三:安装消息队列 四:在C#中Messagequeue class 五:MSMQ-发送消息到远程专用队列 六:例子   一. ...

  4. C#网络编程TCP通信实例程序简单设计

    C#网络编程TCP通信实例程序简单设计 采用自带 TcpClient和TcpListener设计一个Tcp通信的例子 只实现了TCP通信 通信程序截图: 压力测试服务端截图: 俩个客户端链接服务端测试 ...

  5. C# 实现单实例程序

    在我们经常使用的软件中,当我们已经打开后,再次打开时,有的软件不会出现两个.例如有道词典,会将上次的界面显示出来,或者提示我们“该程序已经运行...”.我通过一个简单的C# WPF例子来说明. 首先我 ...

  6. Android L Camera2 API 使用实例程序汇总

    在网上发现几个使用Camera API2开发的实例程序,总结一下方便后续参考: 1.Camera2 Basic : https://github.com/googlesamples/android-C ...

  7. Node.js入门实例程序

    在使用Node.js创建实际“Hello, World!”应用程序之前,让我们看看Node.js的应用程序的部分.Node.js应用程序由以下三个重要组成部分: 导入需要模块: 我们使用require ...

  8. 主成分分析、实例及R语言原理实现

    欢迎批评指正! 主成分分析(principal component analysis,PCA) 一.几何的角度理解PCA -- 举例:将原来的三维空间投影到方差最大且线性无关的两个方向(二维空间). ...

  9. 微信小程序开发语言的选择

    微信使用的开发语言和文件很「特殊」. 小程序所使用的程序文件类型大致分为以下几种: ①WXML(WeiXin Mark Language,微信标记语言) ②WXSS(WeiXin Style Shee ...

随机推荐

  1. 无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel._Application”

    报错内容如下: 无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的COM对象强制转换为接口类型“Microsoft.Office.Inte ...

  2. MySQL增强版命令行客户端连接工具(mycli)

    效果: 安装: http://www.mycli.net/install 官网: http://www.mycli.net/install

  3. 使用LM2576制作数控电源

    http://www.daxia.com/bibis/moredata30_1207792_29862.shtml 图中DA和PWM任选其一, 当DA或PWM输出为0~1.25V时,输出在12.5V~ ...

  4. Java-子类扩展父类功能

    class person {  private String name;  private int age;  public String getname(){   return this.name; ...

  5. mysql 源代码编绎

    http://blog.chinaunix.net/uid-20723616-id-769326.html https://software.intel.com/zh-cn/blogs/2010/08 ...

  6. AWR--service statistics

    近期发现一个奇怪的现象,数据库报告上看负载非常高.可是cpu和等待事件都非常低,不知道消耗的资源跑到哪里去了? Snap Id Snap Time Sessions Cursors/Session B ...

  7. Windows Server 2003 R2 IIS服务的命令行方式重启命令

    iisreset /RESTART 停止后启动   iisreset /START 启动IIS (如果停止)   iisreset /STOP 停止IIS (如果启动)   iisreset /REB ...

  8. 怎么上传自己的代码/项目到自己的github仓库上

    创建新的git仓库   设置新仓库   创建完成后   复制git地址   现在已经有window git客户端了(地址:https://desktop.github.com/),但还是建议用git命 ...

  9. Android之MVC——Model通知View去更新(实用)

    下面两段标红加深的代码是重点: import android.app.Activity; import android.os.Bundle; import android.view.View; imp ...

  10. chromium对网页获取favicon

    每一个网页都有一个favicon,在历史记录的保存中须要用到.在content文件夹下,这个没有实现. 以下说一下我的实现过程: web_contents_impl.cc文件里有方法:WebConte ...