1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX_SIZE 10
  4. /* 用一个动态数组来实现队列 */
  5.  
  6. typedef struct Queue {
  7. int Capacity;
  8. int Front;
  9. int Rear;
  10. int Size;
  11. int data[MAX_SIZE];
  12. } Queue;
  13.  
  14. void Error(char *error) {
  15. printf("%s",error);
  16. }
  17.  
  18. void FatalError(char *fatalerror) {
  19. printf("%s",fatalerror);
  20. }
  21.  
  22. int IsEmpty(Queue *Q) {
  23. return Q->Size == ;
  24. }
  25.  
  26. int IsFull(Queue *Q) {
  27. return Q->Size == Q->Capacity;
  28. }
  29.  
  30. void Init( Queue *Q ) {
  31. Q->Size = ;
  32. Q->Front = ;
  33. Q->Rear = ;
  34. Q->Capacity = MAX_SIZE;
  35. }
  36.  
  37. static int Succ(int value,Queue *Q) {
  38. if(++value == Q->Capacity) {
  39. value = ;
  40. }
  41. return value;
  42. }
  43.  
  44. void Enqueue(int X,Queue *Q) {
  45. if( IsFull( Q ) )
  46. FatalError("Full queue");
  47. else {
  48. Q->Size++;
  49. Q->Rear = Succ(Q->Rear,Q);
  50. Q->data[ Q->Rear ] = X;
  51. }
  52. }
  53.  
  54. void Dequeue(Queue *Q) {
  55. if(IsEmpty(Q))
  56. FatalError("Empty queue");
  57. else {
  58. Q->Size--;
  59. Q->Front = Succ(Q->Front,Q);
  60. }
  61. }
  62.  
  63. int FrontAndDequeue(Queue *Q) {
  64. int Tmp;
  65. if(IsEmpty(Q))
  66. Error("Empty queue");
  67. else {
  68. Q->Size--;
  69. Tmp = Q->data[Q->Front];
  70. Q->Front = Succ(Q->Front,Q);
  71. return Tmp;
  72. }
  73. }
  74.  
  75. void DisposeQueue( Queue *Q ) {
  76. free(Q->data);
  77. free(Q);
  78. }
  79.  
  80. main() {
  81. Queue *q ;
  82. Init(q);
  83. int i;
  84. for(i = ; i <MAX_SIZE; i++) {
  85. Enqueue(i,q);
  86. }
  87. for(i = ; i <MAX_SIZE; i++) {
  88. printf("%d\n",FrontAndDequeue(q));
  89. }
  90. }

C语言实现常用数据结构——队列的更多相关文章

  1. C语言实现常用数据结构——链表

    #include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node *next; ...

  2. C语言实现常用数据结构——堆

    #include<stdio.h> #include<stdlib.h> #define CAPACITY 20 /*堆有两个性质: * 1.结构性:堆必须是一颗完全二叉树 * ...

  3. C语言实现常用数据结构——图

    #include<stdio.h> #include<stdlib.h> #define SIZE 20 #define LENGTH(a) (sizeof(a)/sizeof ...

  4. C语言实现常用数据结构——二叉树

    #include<stdio.h> #include<stdlib.h> #define SIZE 10 typedef struct Tree { int data; str ...

  5. C语言实现常用数据结构——栈

    #include<stdio.h> #include<stdlib.h> //用链表实现栈 typedef struct Node { int data; struct Nod ...

  6. C语言数据结构-队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作

    1.数据结构-队列的实现-C语言 //队列的存储结构 #define MAXSIZE 100 typedef struct { int* base; //基地址 int _front; //头指针 i ...

  7. 1. C语言中的数据结构.md

    C语言内建数据结构类型 整型 整型数据是最基本的数据类型,不过从整形出发衍生出好几种integer-like数据结构,譬如字符型,短整型,整型,长整型.他们都是最基本的方式来组织的数据结构,一般是几位 ...

  8. Java 集合框架(常用数据结构)

    早在Java 2中之前,Java就提供了特设类.比如:向量(Vector).栈(Stack).字典(Dictionary).哈希表(Hashtable)这些类(数据结构)用来存储和操作对象组.虽然这些 ...

  9. C++常用数据结构的实现

    常用数据结构与算法的实现.整理与总结 我将我所有数据结构的实现放在了github中:Data-Structures-Implemented-By-Me 常用数据结构与算法的实现.整理与总结 KMP字符 ...

随机推荐

  1. less循环写css工具类

    //margin-right=================.mr(100); .mr(@n, @i: 1) when (@i =< @n) { .mr-@{i} { margin-right ...

  2. wpf控件开发基础(2) -属性系统(1)

    原文:wpf控件开发基础(2) -属性系统(1) 距离上篇写的时间有1年多了.wpf太大,写的东西实在太多,我将依然围绕着自定义控件来展开与其相关的技术点. 也欢迎大家参与讨论.这篇我们将要讨论的是W ...

  3. WPF 3D 模型旋转

    原文:WPF 3D 模型旋转 WPF 是 Microsoft 在 Framework3.0 中支持的一种技术,它能作出很绚丽的界面,同时它也支持3D的操作.在3D操作主要包括平移(Translate) ...

  4. 面向对象举例(一) —— 顶点(vertex)、边(edge)与图(graph)

    Graph: class Graph(dict): def __init__(self, vs=[], es=[]): for v in vs: self.add_vertex(v) for e in ...

  5. WPF无边框捕获消息改变窗口大小

    原文:WPF无边框捕获消息改变窗口大小 文章大部分转载自http://blog.csdn.net/fwj380891124,如有问题,请联系删除  最近一直在学习 WPF,看着别人做的WPF程序那么漂 ...

  6. Android - 小的特点 - 使用最新版本ShareSDK手册分享(分享自己定义的接口)

    前太实用Share SDK很快分享,但官员demo快捷共享接口已被设置死,该公司的产品还设计了自己的份额接口,这需要我手动共享. 读了一堆公文,最终写出来,行,废话,进入主题. 之前没实用过Share ...

  7. VC绘制控件如何防止闪烁

    理论上不管什么控件都适用,方法如下: 新建一个MFC类,继承原来的控件类型,对于VC自动生成的类进行如下改动: 首先覆盖 OnEraseBkgnd() 防止擦除时填涂背景: BOOL CXXXCtrl ...

  8. Java 阅读TXT文件

    public class GenCategoryAttrItemHandler { private final static String INPUT_FILE_PATH = "input/ ...

  9. xmpp和OpenFire示例,即时聊天室,支持离线消息

    让我说说为什么写这个博客,这是因为我在上周末的研究XMPP和OpenFire,从互联网上下载Demo,但跑不起来.它花了很长的时间.它被改造.抬高.篇博文也是希望后边学习XMPP和OpenFire的同 ...

  10. C# API 获取系统DPI缩放倍数跟分辨率大小

    原文:C# API 获取系统DPI缩放倍数跟分辨率大小 using System; using System.Drawing; using System.Runtime.InteropServices ...