数据结构【一】:简单队列simple queue
简单的FIFO队列实现,非线程安全!
1.queue.h : abstract data type queue
#ifndef CUR_QUEUE_H
#define CUR_QUEUE_H
#include<stdlib.h>
struct node{
int value;
struct node * next;
}; typedef struct queue{
int max,cur;
struct node * head, * tail;
}queue; extern queue* empty_queue(int _max);
extern int queue_free(queue *q);
extern int is_empty(const queue *q);
extern int is_full(const queue *q);
extern int enqueue(struct node *item, queue *q);
extern struct node* dequeue(queue *q); #endif
2.queue.c
#include "queue.h" queue* empty_queue(int _max)
{
queue *q = malloc(sizeof(queue));
q->head = q->tail = NULL;
q->max = _max;
q->cur = ;
return q;
} int queue_free(queue *q)
{
while(!is_empty(q))
free(dequeue(q));
free(q);
} int is_empty(const queue *q)
{
return q->cur == ;
} int is_full(const queue *q)
{
return q->cur == q->max;
} int enqueue(struct node *item, queue *q)
{
if(is_full(q)) return -;
if(is_empty(q))
q->head = q->tail = item;
else
{
q->tail->next = item;
q->tail = item;
}
q->cur++;
return ;
} struct node* dequeue(queue *q)
{
if(is_empty(q)) return NULL;
struct node * temp = q->head;
q->head = q->head->next;
q->cur--;
return temp;
}
3.main.c
#include<stdio.h>
#include<stdlib.h>
#include "queue.h" void main()
{
int i;
queue *q = empty_queue(); for(i=; i<=; i++){ struct node * item = (struct node *)malloc(sizeof(struct node));
item->value = i;
item->next = NULL; printf("is_full : %d\n",is_full(q));
if(is_full(q)) {
printf("queue is full\n");
printf("enqueue : %d\n",enqueue(item,q));
break;
}
printf("enqueue : %d\n",enqueue(item,q));
} while(!is_empty(q))
{
struct node * item = dequeue(q);
if(item != NULL){
printf("value : %d ,current : %d\n", item->value,q->cur);
free(item);
}
}
queue_free(q);
}
测试结果:

数据结构【一】:简单队列simple queue的更多相关文章
- 数据结构——线性表——队列(queue)
队列也是一种特殊的线性表,它的特点是先入先出(FIFO,即first in first out).它的意思也很直观,想象一下排队买票,先排的人先买(插队是不对的,所以别去想).它也是很常用的数据结构, ...
- 数据结构14:队列(Queue),“先进先出”的数据结构
队列是线性表的一种,在操作数据元素时,和栈一样,有自己的规则:使用队列存取数据元素时,数据元素只能从表的一端进入队列,另一端出队列,如图1. 图1 队列示意图 称进入队列的一端为“队尾”:出队列的一端 ...
- python 队列(QUEUE)
QUEUE python中多线程编程的数据结构 基本FIFO队列 class Queue.Queue(maxsize=0) 先进先出,maxsize为队列中能存放的数据个数上限. import Que ...
- simple queue(简单队列)
Virtual host-虚拟主机 虚拟主机,用于进行逻辑隔离,是最上层的路由,类似于redis的16个db,是一种逻辑上的隔离 一个virtualhost里面可以有若干个Exchange和Queue ...
- HTTPSQS(HTTP Simple Queue Service)消息队列
HTTPSQS(HTTP Simple Queue Service)是一款基于 HTTP GET/POST 协议的轻量级开源简单消息队列服务,使用 Tokyo Cabinet 的 B+Tree Key ...
- 第二十四篇 玩转数据结构——队列(Queue)
1.. 队列基础 队列也是一种线性结构: 相比数组,队列所对应的操作数是队列的子集: 队列只允许从一端(队尾)添加元素,从另一端(队首)取出元素: 队列的形象化描述如下图: 队列是一种先进 ...
- C++数据结构之链式队列(Linked Queue)
C++数据结构之链式队列,实现的基本思想和链式栈的实现差不多,比较不同的一点也是需要注意的一点是,链式队列的指向指针有两个,一个是队头指针(front),一个是队尾指针(rear),注意指针的指向是从 ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)
循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...
- PHP实现队列(Queue)数据结构
队列(Queue),是一种特殊的先进先出线性表,其只能在前端进行删除操作(一般称为出队),在后端进行插入操作(一般称为入队).进行删除操作的端称为队头,进行插入操作的端称为队尾.队列,是按照先进先出或 ...
随机推荐
- Symbol Table
[Symbol Table] In order for GDB to be useful to us, it needs to be able to refer to variable and fun ...
- 成功获取并更改中兴F660光猫的超级用户密码解除四台限制
上次雷雨后更换的中兴的F660光猫还是很不错的,很稳定,不过超级密码确实记不住,找了些资料,今天成功的更改了密码,简要的写出过程以备下次参考: 第一步:获取超级密码(已知用户名telecomadmin ...
- MFC中消息响应机制
由于视类窗口始终覆盖在框架类窗口之上,因此所有操作,包括鼠标单击.鼠标移动等操作都只能由视类窗口捕获.一个MFC消息响应函数在程序中有三处相关信息:函数原型.函数实现和以及用来关联消息和消息响应函数的 ...
- 面向过程MySQL数据库链接操作
刚好今天复习到这个章节,将就发布出来,就当是为自己复习了 //链接数据库 $link = mysqli_connect('localhost/IP地址','用户名','密码','数据库名'); //设 ...
- Hadoop伪分布配置与基于Eclipse开发环境搭建
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 系列文章--SharePoint 开发教程
SharePoint 2013 图文开发系列之入门教程 学习地址:http://www.cnblogs.com/jianyus/p/3461719.html 里面有2007.2010.2013各个版本 ...
- Java *字格
class XingDemo { public static void main(String[] args) { int j = 0; int i = 0; int k = 0; for(i = 0 ...
- CodeForces 163A Substring and Subsequence dp
A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...
- HDU 4099 Revenge of Fibonacci(高精度+字典树)
题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...
- 关于SPA及RPA
SPA Request-------->Callback (page onload,execute <script>)-------->call func RPA URL re ...