作者: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)的更多相关文章

  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. python 笔记1:安装python;eclipse中安装配置pydev

    1  下载安装python. 官网:https://www.python.org/downloads/     根据自己的操作系统选择需要的版本下载并安装. 我的电脑操作系统windows xp的,只 ...

  2. json对象转换为json字符串

    今天浏览网页的时候看到这个题也是一道很经典的题目了 var str ='aaaaaaajsdjdfkdkg'; ,]; var obj={}; ;i<str.length;i++){ if(!o ...

  3. iOS 剪贴板基本知识

    在iOS 中下面三个控件,自身就有复制-粘贴功能 1.UITextView 2.UITextField 3.UIWebView UIKit framework提供了几个类和协议方便我们在自己的应用程序 ...

  4. python模块之os

    os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如说:绝对路径,父目录-- os.sep可以取代操作系统特定的路径分隔符.windows下为 "\ ...

  5. 在 Xcode 6 中使用矢量图( iPhone 6 置配 UI)

    在 Xcode 6 中使用矢量图( iPhone 6 置配 UI) (本文转载:http://iosdeveloper.diandian.com/post/2014-09-25/40063062789 ...

  6. 从零开始学习Node.js例子四 多页面实现数学运算 续一(使用connect和express框架)

    1.使用connect框架 .use方法用于绑定中间件到connect服务器,它会配置一系列在接到请求时调用的中间件模块,此例中我们要配置的中间件有favicon logger static rout ...

  7. 帝国CMS灵动标签e:loop

    头条调用方法 1 [e:loop={'selfinfo',5,13,0,'firsttitle=2'}]<a href="<?=$bqsr[titleurl]?>" ...

  8. cf340D Bubble Sort Graph

    link:http://codeforces.com/problemset/problem/340/D 感觉很好的一道题目. 认真思考,发现,逆序的数字对一定有边相连.所以,题目要求没有边相连的最大的 ...

  9. Cookie初识篇

    一.什么是Cookie Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密).定义于RFC2109和2965都 ...

  10. Windows 10的TPM模块到底是不是美国全球监控体系的奠基石?

    http://bbs.pediy.com/showthread.php?t=202638 http://www.zhihu.com/topic/19671262/newest 2015年3月18日,微 ...