作者: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. 用qpython3写一个最简单的发送短信的程序

    到目前为止并没有多少手机应用是用python开发的,不过qpython可以作为一个不错的玩具推荐给大家来玩. 写一个最简单的发送短信的程序,代码如下: #-*-coding:utf8;-*- #qpy ...

  2. Notification NotificationManager RemoteViews PendingIntent

    Notification和NotificationManager :可以用来实现可视化的消息通知等,比如:下载进度显示,消息显示,广播的内容等 RemoteViews :用于创建自定义的Notific ...

  3. Ubuntu中添加eclipse

    环境:Ubuntu 14.04 步骤: 1.安装配置JDK,详见 http://my.oschina.net/u/1407116/blog/227084 2.下载eclipse 从官网http://w ...

  4. 隐匿在iOS文件系统中的隐私信息

    说明: . 本文仅供安全学习及教学用途,提及的部分技术带有攻击性,请确保合法使用. . “这些都不是我干的,我就负责说出来.” . 图片仅供参考. . 本文所讨论的内容适用于iOS .4环境,其应用程 ...

  5. C#中this在扩展方法的应用

    给类添加扩展方法 1.定义一个类Service public class Service { private string _name; public string Name { get { retu ...

  6. 不同vlan之间的相互访问

    拓扑图: 用到的命令: 给端口的vlan <sw1>用户模式 切换到系统模式 system-view 交换机名称 sysname swj1 创建vlan 3 端口模式选择int g0/0/ ...

  7. Tomcat 安装--小白教程

    因为要进行微信公众号的开发模式,所以需要安装Tomcat Web服务器,现在就把我的安装过程写下来,希望可以帮到有需要的人~首先,我们需要下载tomcat的安装包,直接去官网就好啦,http://to ...

  8. c语言二叉树基本操作

    编译器为vs2013 #include "stdafx.h" #include<malloc.h> #include<stdlib.h> #define O ...

  9. poj3461 字符串匹配 熟悉kmp算法第一题

     题意:  计算一个字符串在另一个字符串中出现的次数. #include<cstdio> #include<cstring> #include<algorithm> ...

  10. python百分比数比较大小

    python是无法识别百分比的,估计你的百分比是string,所以需要转成int # !/usr/bin/python3.4 # -*- coding: utf-8 -*- # 百分数转为int de ...