作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明。谢谢!

队列(queue)是一个简单而常见的数据结构。队列也是有序的元素集合。队列最大的特征是First In, First Out (FIFO,先进先出),即先进入队列的元素,先被取出。这一点与栈(stack)形成有趣的对比。队列在生活中很常见,排队买票、排队等车…… 先到的人先得到服务并离开队列,后来的人加入到队列的最后。队列是比较公平的分配有限资源的方式,可以让队列的人以相似的等待时间获得服务。

队列支持两个操作,队首的元素离开队列(dequeue),和新元素加入队尾(enqueue)。

队列

队列在计算机中应用很广泛。一个经典的应用是消息队列(参考Linux进程间通信),实际上就是利用队列来分配有限的进程。还有FIFO文件(哦,你可以看到,这种文件叫做FIFO,肯定是和队列有关),用以实现管道传输。再比如,我们将多个打印任务发送给打印机,打印机也是使用队列来安排任务的顺序。

队列的C实现 (基于表)

和栈相似,队列也可以有多种实现方式,这里是基于单链表的实现。

表(list)中的实现方式略有不同的是,这里的head node有两个指针,一个(next)指向下一个元素,一个(end)指向队列的最后一个元素。这样做的目的是方便我们找到队尾,以方便的进行enqueue操作。我们依然可以使用之前定义的表,在需要找到队尾的时候遍历搜索到最后一个元素。

用于队列的表

下面是代码:

  1. /* By Vamei */
  2. /* use single-linked list to implement queue */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct node *position;
  7. typedef int ElementTP;
  8.  
  9. // point to the head node of the list
  10. typedef struct HeadNode *QUEUE;
  11.  
  12. struct node {
  13. ElementTP element;
  14. position next;
  15. };
  16.  
  17. /*
  18. * CAUTIOUS: "HeadNode" is different from "node",
  19. * it's another struct
  20. * end: points to the last value in the queue
  21. */
  22. struct HeadNode {
  23. ElementTP element;
  24. position next;
  25. position end;
  26. };
  27.  
  28. /*
  29. * Operations
  30. */
  31. QUEUE init_queue(void);
  32. void delete_queue(QUEUE);
  33. void enqueue(QUEUE, ElementTP);
  34. ElementTP dequeue(QUEUE);
  35. int is_null(QUEUE);
  36.  
  37. /*
  38. * Test
  39. */
  40. void main(void)
  41. {
  42. ElementTP a;
  43. int i;
  44. QUEUE qu;
  45. qu = init_queue();
  46.  
  47. enqueue(qu, 1);
  48. enqueue(qu, 2);
  49. enqueue(qu, 8);
  50. printf("Queue is null? %d\n", is_null(qu));
  51. for (i=0; i<3; i++) {
  52. a = dequeue(qu);
  53. printf("dequeue: %d\n", a);
  54. }
  55.  
  56. printf("Queue is null? %d\n", is_null(qu));
  57. delete_queue(qu);
  58. }
  59.  
  60. /*
  61. * initiate the queue
  62. * malloc the head node.
  63. * Head node doesn't store valid data
  64. * head->next is the first node in the queue.
  65. */
  66. QUEUE init_queue(void)
  67. {
  68. QUEUE hnp;
  69. hnp = (QUEUE) malloc(sizeof(struct HeadNode));
  70. hnp->next = NULL; // qu->next is the first node
  71. hnp->end = NULL;
  72. return hnp;
  73. }
  74.  
  75. /*
  76. * dequeue all elements
  77. * and then delete head node
  78. */
  79. void delete_queue(QUEUE qu)
  80. {
  81. while(!is_null(qu)) {
  82. dequeue(qu);
  83. }
  84. free(qu);
  85. }
  86.  
  87. /*
  88. * enqueue a value to the end of the queue
  89. */
  90. void enqueue(QUEUE qu, ElementTP value)
  91. {
  92. position np, oldEnd;
  93. oldEnd = qu->end;
  94.  
  95. np = (position) malloc(sizeof(struct node));
  96. np->element = value;
  97. np->next = NULL;
  98.  
  99. /* if queue is empyt, then oldEnd is NULL */
  100. if (oldEnd) {
  101. oldEnd->next = np;
  102. }
  103. else {
  104. qu->next = np;
  105. }
  106.  
  107. qu->end = np;
  108. }
  109.  
  110. /*
  111. * dequeue the first value
  112. */
  113. ElementTP dequeue(QUEUE qu)
  114. {
  115. ElementTP element;
  116. position first, newFirst;
  117. if (is_null(qu)) {
  118. printf("dequeue() on an empty queue");
  119. exit(1);
  120. }
  121. else {
  122. first = qu->next;
  123. element = first->element;
  124. newFirst = first->next;
  125. qu->next = newFirst;
  126. free(first);
  127. return element;
  128. }
  129. }
  130.  
  131. /*
  132. * check: queue is empty?
  133. */
  134. int is_null(QUEUE qu)
  135. {
  136. return (qu->next == NULL);
  137. }

运行结果如下:

Queue is null? 0
dequeue: 1
dequeue: 2
dequeue: 8
Queue is null? 1

总结

队列,FIFO

enqueue, dequeue

纸上谈兵:队列(queue)的更多相关文章

  1. Python进阶【第二篇】多线程、消息队列queue

    1.Python多线程.多进程 目的提高并发 1.一个应用程序,可以有多进程和多线程 2.默认:单进程,单线程 3.单进程,多线程 IO操作,不占用CPU python的多线程:IO操作,多线程提供并 ...

  2. Java中的队列Queue,优先级队列PriorityQueue

    队列Queue 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作.该接口扩展了java.util.Collection接口. Queue使用时要尽量避免Collecti ...

  3. jquery 的队列queue

    使用示列代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  4. Windows Azure Service Bus (2) 队列(Queue)入门

    <Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...

  5. Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue

    <Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...

  6. (C#)使用队列(Queue)解决简单的并发问题

    (C#)使用队列(Queue)解决简单的并发问题 2015-07-16 13:04 13265人阅读 评论(8) 收藏 举报  分类: Asp.Net(8)  版权声明:本文为博主原创文章,未经博主允 ...

  7. STL中的单向队列queue

    转载自:http://blog.csdn.net/morewindows/article/details/6950917 stl中的queue指单向队列,使用时,包含头文件<queue>. ...

  8. java09 队列Queue与Deque

    队列Queue与Deque. Enumeration Hashtable与Hashtable子类Properties(资源配置文件) 引用类型(强.软.弱.虚)与WeakHashMap Identit ...

  9. 队列Queue和栈

    1.队列Queue是常用的数据结构,可以将队列看成特殊的线性表,队列限制了对线性表的访问方式,只能从线性表的一段添加(offer)元素, 从另一段取出(poll)元素,队列遵循先进先出的原则. 2.J ...

  10. 消息队列Queue大全

    消息队列Queue大全 (http://queues.io/) 作业队列,消息队列和其他队列.几乎所有你能想到的都在这. 关于 那里有很多排队系统.他们每个人都不同,是为解决某些问题而创建的.这个页面 ...

随机推荐

  1. css中的display以及position属性

    我们都知道,元素分为行内元素和块级元素,在页面布局中,我们常常需要让行内元素具有块级元素的特性,或者使块级元素转换成行内元素,这就要使用我们的display属性了. 我们先定义三个div: 以上的三个 ...

  2. UIkit框架之uUInavigationController

    1.继承链:UIviewcontroller:uiresponder:NSObject 2.如果你想使用一些动画转换,可以遵守 UINavigationControllerDelegate 3.创建导 ...

  3. SVN小贴士

    我辛辛苦苦写的到哪里了? SVN小贴士SVN服务器上的代码项目组公用,你的每一个提交都会体现给项目组每个人,所以提交要慎重,要注意避免代码冲突,使用SVN小贴士: 1.提前宣布开发计划,保持项目组成员 ...

  4. NPOI设置Excel保护

    有时,我们可能需要某些单元格只读,如在做模板时,模板中的数据是不能随意让别人改的.在Excel中,可以通过“审阅->保护工作表”来完成,如下图:  那么,在NPOI中有没有办法通过编码的方式达到 ...

  5. CodeBlocks 中fopen函数不支持命令 “r”

    //codeblocks #include<stdio.h> #include<stdlib.h> void main(void) { FILE *fp=NULL; if((f ...

  6. webservice可以访问但是不能调用方法

        <webServices>               <protocols>                 <add   name="HttpSoa ...

  7. LA 3938 动态最大连续和 线段树

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  8. 【Gerrit】gerrit server搭建

    Part 1  Gerrit Prerequisites: 1.Java JDK>1.7 2.Git 3.SSH server 4.DB part 2 Set local gerrit serv ...

  9. ORACLE PL/SQL编程详解

    ORACLE PL/SQL编程详解 编程详解 SQL语言只是访问.操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发.PL /SQL是一种高级数据库程序设 ...

  10. Alpha版本——Postmortem会议

    No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 设想和目标 1.我们的软件要解决什么 ...