纸上谈兵:队列(queue)
作者: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操作。我们依然可以使用之前定义的表,在需要找到队尾的时候遍历搜索到最后一个元素。
用于队列的表
下面是代码:

- /* By Vamei */
- /* use single-linked list to implement queue */
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node *position;
- typedef int ElementTP;
- // point to the head node of the list
- typedef struct HeadNode *QUEUE;
- struct node {
- ElementTP element;
- position next;
- };
- /*
- * CAUTIOUS: "HeadNode" is different from "node",
- * it's another struct
- * end: points to the last value in the queue
- */
- struct HeadNode {
- ElementTP element;
- position next;
- position end;
- };
- /*
- * Operations
- */
- QUEUE init_queue(void);
- void delete_queue(QUEUE);
- void enqueue(QUEUE, ElementTP);
- ElementTP dequeue(QUEUE);
- int is_null(QUEUE);
- /*
- * Test
- */
- void main(void)
- {
- ElementTP a;
- int i;
- QUEUE qu;
- qu = init_queue();
- enqueue(qu, 1);
- enqueue(qu, 2);
- enqueue(qu, 8);
- printf("Queue is null? %d\n", is_null(qu));
- for (i=0; i<3; i++) {
- a = dequeue(qu);
- printf("dequeue: %d\n", a);
- }
- printf("Queue is null? %d\n", is_null(qu));
- delete_queue(qu);
- }
- /*
- * initiate the queue
- * malloc the head node.
- * Head node doesn't store valid data
- * head->next is the first node in the queue.
- */
- QUEUE init_queue(void)
- {
- QUEUE hnp;
- hnp = (QUEUE) malloc(sizeof(struct HeadNode));
- hnp->next = NULL; // qu->next is the first node
- hnp->end = NULL;
- return hnp;
- }
- /*
- * dequeue all elements
- * and then delete head node
- */
- void delete_queue(QUEUE qu)
- {
- while(!is_null(qu)) {
- dequeue(qu);
- }
- free(qu);
- }
- /*
- * enqueue a value to the end of the queue
- */
- void enqueue(QUEUE qu, ElementTP value)
- {
- position np, oldEnd;
- oldEnd = qu->end;
- np = (position) malloc(sizeof(struct node));
- np->element = value;
- np->next = NULL;
- /* if queue is empyt, then oldEnd is NULL */
- if (oldEnd) {
- oldEnd->next = np;
- }
- else {
- qu->next = np;
- }
- qu->end = np;
- }
- /*
- * dequeue the first value
- */
- ElementTP dequeue(QUEUE qu)
- {
- ElementTP element;
- position first, newFirst;
- if (is_null(qu)) {
- printf("dequeue() on an empty queue");
- exit(1);
- }
- else {
- first = qu->next;
- element = first->element;
- newFirst = first->next;
- qu->next = newFirst;
- free(first);
- return element;
- }
- }
- /*
- * check: queue is empty?
- */
- int is_null(QUEUE qu)
- {
- return (qu->next == NULL);
- }

运行结果如下:
Queue is null? 0
dequeue: 1
dequeue: 2
dequeue: 8
Queue is null? 1
总结
队列,FIFO
enqueue, dequeue
纸上谈兵:队列(queue)的更多相关文章
- Python进阶【第二篇】多线程、消息队列queue
1.Python多线程.多进程 目的提高并发 1.一个应用程序,可以有多进程和多线程 2.默认:单进程,单线程 3.单进程,多线程 IO操作,不占用CPU python的多线程:IO操作,多线程提供并 ...
- Java中的队列Queue,优先级队列PriorityQueue
队列Queue 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作.该接口扩展了java.util.Collection接口. Queue使用时要尽量避免Collecti ...
- jquery 的队列queue
使用示列代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- Windows Azure Service Bus (2) 队列(Queue)入门
<Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...
- Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue
<Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...
- (C#)使用队列(Queue)解决简单的并发问题
(C#)使用队列(Queue)解决简单的并发问题 2015-07-16 13:04 13265人阅读 评论(8) 收藏 举报 分类: Asp.Net(8) 版权声明:本文为博主原创文章,未经博主允 ...
- STL中的单向队列queue
转载自:http://blog.csdn.net/morewindows/article/details/6950917 stl中的queue指单向队列,使用时,包含头文件<queue>. ...
- java09 队列Queue与Deque
队列Queue与Deque. Enumeration Hashtable与Hashtable子类Properties(资源配置文件) 引用类型(强.软.弱.虚)与WeakHashMap Identit ...
- 队列Queue和栈
1.队列Queue是常用的数据结构,可以将队列看成特殊的线性表,队列限制了对线性表的访问方式,只能从线性表的一段添加(offer)元素, 从另一段取出(poll)元素,队列遵循先进先出的原则. 2.J ...
- 消息队列Queue大全
消息队列Queue大全 (http://queues.io/) 作业队列,消息队列和其他队列.几乎所有你能想到的都在这. 关于 那里有很多排队系统.他们每个人都不同,是为解决某些问题而创建的.这个页面 ...
随机推荐
- css中的display以及position属性
我们都知道,元素分为行内元素和块级元素,在页面布局中,我们常常需要让行内元素具有块级元素的特性,或者使块级元素转换成行内元素,这就要使用我们的display属性了. 我们先定义三个div: 以上的三个 ...
- UIkit框架之uUInavigationController
1.继承链:UIviewcontroller:uiresponder:NSObject 2.如果你想使用一些动画转换,可以遵守 UINavigationControllerDelegate 3.创建导 ...
- SVN小贴士
我辛辛苦苦写的到哪里了? SVN小贴士SVN服务器上的代码项目组公用,你的每一个提交都会体现给项目组每个人,所以提交要慎重,要注意避免代码冲突,使用SVN小贴士: 1.提前宣布开发计划,保持项目组成员 ...
- NPOI设置Excel保护
有时,我们可能需要某些单元格只读,如在做模板时,模板中的数据是不能随意让别人改的.在Excel中,可以通过“审阅->保护工作表”来完成,如下图: 那么,在NPOI中有没有办法通过编码的方式达到 ...
- CodeBlocks 中fopen函数不支持命令 “r”
//codeblocks #include<stdio.h> #include<stdlib.h> void main(void) { FILE *fp=NULL; if((f ...
- webservice可以访问但是不能调用方法
<webServices> <protocols> <add name="HttpSoa ...
- LA 3938 动态最大连续和 线段树
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- 【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 ...
- ORACLE PL/SQL编程详解
ORACLE PL/SQL编程详解 编程详解 SQL语言只是访问.操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发.PL /SQL是一种高级数据库程序设 ...
- Alpha版本——Postmortem会议
No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 设想和目标 1.我们的软件要解决什么 ...