#include <stdio.h>
#include <stdlib.h>
#include "queue.h"

int main()
{
 int i;
 Type x;
 Type arr[] = {3,1,2,5,7,9};
 QUEUE *q = NULL;

q = CreateQueue(10);
 if(NULL == q)
  return -1;
 
 for(i = 0; i < sizeof(arr)/sizeof(*arr); i++)
 {
  EnQueue(q, arr + i);
 }
 FrontQueue(q, &x);
 printf("x = %d\n", x);

DisptoryQueue(q);
 return 0;
}
---------------------------------------------------------------

#ifndef _QUEUE_H__
#define _QUEUE_H__

struct node;
typedef int Type;
typedef struct node QUEUE;

QUEUE *CreateQueue(int);
void QueueMakeEmpty(QUEUE *);
int QueueIsEmpty(QUEUE *);
int QueueIsFull(QUEUE *);
int EnQueue(QUEUE *, const Type *);
int DeQueue(QUEUE *);
int FrontQueue(QUEUE *, Type *);
int FrontAndDeQueue(QUEUE *, Type *);
void DisptoryQueue(QUEUE *);

struct node{
 Type *data;
 int capacity;
 int front;
 int rear;
 int size;
};

#endif
-------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include "queue.h"

QUEUE *CreateQueue(int size)
{
 QUEUE *q = malloc(sizeof(*q));
 if(NULL == q)
  return NULL;
 q->data = malloc(sizeof(Type)*size); //队列的长度,队列的成员个数
 if(NULL == q->data)
 {
  free(q);
  return NULL;
 }
 q->capacity = size; //队列容量
 QueueMakeEmpty(q);
 return q;
}
void QueueMakeEmpty(QUEUE *q)
{
 q->size = 0;
 q->front = 1;
 q->rear = 0;
}
int QueueIsEmpty(QUEUE *q)
{
 return q->size == 0;
}
int QueueIsFull(QUEUE *q)
{
 return q->size == q->capacity;
}
static int repeat(QUEUE *q, int rear) //队列队尾入队,
{
 if(++rear == q->capacity)
  rear = 0;
 return rear;
}
int EnQueue(QUEUE *q, const Type *x)
{
 if(QueueIsFull(q))
  return -1;
 q->rear = repeat(q, q->rear); //每次入队成功后,队尾rear置0.
 q->data[q->rear] = *x;
 q->size++;
 return 0;
}
int DeQueue(QUEUE *q) //出队
{
 if(QueueIsEmpty(q))
  return -1;
 q->front = repeat(q, q->front);
 q->size--;
 return 0;
}
int FrontQueue(QUEUE *q, Type *x) //查看队首
{
 if(QueueIsEmpty(q))
  return -1;
 *x = q->data[q->front];
 return 0;
}
int FrontAndDeQueue(QUEUE *q, Type *x) //查看队首并出队
{
 if(FrontQueue(q, x) == 0)
  return DeQueue(q);
 return -1;
}
void DisptroyQueue(QUEUE *q)
{
 free(q->data);
 free(q);
}

QUEUE——队列(procedure)的更多相关文章

  1. C#基础---Queue(队列)的应用

       Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...

  2. 第19章 queue队列容器

    /* 第19章 queue队列容器 19.1 queue技术原理 19.2 queue应用基础 19.3 本章小结 */ // 第19章 queue队列容器 // 19.1 queue技术原理 // ...

  3. atitit. java queue 队列体系and自定义基于数据库的队列总结o7t

    atitit. java queue 队列体系and自定义基于数据库的队列总结o7t 1. 阻塞队列和非阻塞队列 1 2. java.util.Queue接口, 1 3. ConcurrentLink ...

  4. C#部分---特殊集合:stack栈集合、queue队列集合、哈希表集合。

    1.stack栈集合:又名 干草堆集合 栈集合 特点:(1)一个一个赋值 一个一个取值(2)先进后出实例化 初始化 Stack st = new Stack(); //添加元素用push st.Pus ...

  5. 实现一个线程安全的Queue队列

    使用装饰者模式实现一个线程安全的Queue队列. public class SynchronizedQueue<E> implements Queue<E>, Serializ ...

  6. Python自动化运维之16、线程、进程、协程、queue队列

    一.线程 1.什么是线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位. 一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行 ...

  7. Stack集合 Queue队列集合 Hashtable哈希表

    Stack集合 干草堆集合 栈集合 栈;stack,先进后出,一个一个赋值,一个一个取值,安装顺序来. 属性和方法 实例化 初始化 Stack st = new Stack(); 添加元素 个数 Co ...

  8. Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块

    Python第十五天  datetime模块 time模块   thread模块  threading模块  Queue队列模块  multiprocessing模块  paramiko模块  fab ...

  9. python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)

    今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系 ...

  10. (8)进程---Queue队列

    # IPC Inter-Process Communication # 实现进程之间通信的两种机制: # 管道 Pipe 用的很少 # 队列 Queue 队列的特征:现进先出,栈属于后进后出 基本语法 ...

随机推荐

  1. iOS Core Animation学习总结(1)--CALayer常用属性

    图层是core animation的基础, UIView之所以能显示在屏幕上,靠的是其内部的这个图层,即每个UIView 都有 CALayer,可通过UIView.layer或者[UIView lay ...

  2. UITableView编写可以添加,删除,移动的物品栏(二)

    MyTableViewCell.h文件(自定义ViewCell)的内容: MyTableViewCell.m的内容

  3. 由C到C++的学习 ----Essential C++

    一.array[] 与 vector<int> vector1 1 array[] <- 此中要填写数组的大小,而且array[]自己是不知道自己的大小的 2.1 vector< ...

  4. 使用jquer获取当前时间,并赋值到input上。

    今天,做一个项目,需要将自动获取的时间能够赋值到input上.我一开始试用javascript写的,写之后使用很多办法都赋值失败,最后使用了jquery写了出来,下面附上代码. <script ...

  5. Jquery常用功能

    jQuery 1.4给开发者带来了很多值得兴奋的新特性,同时使用jQuery的人也越来越多,为了方便大家对jQuery的使用,下面列出了一些jQuery使用技巧.比如有禁止右键点击.隐藏搜索文本框文字 ...

  6. 深入了解join用法

    最近面试经常被问到inner join, right join , left join  今晚决定搞清楚这些: 首先先创建两个表: CREATE TABLE Persons ( Id_P int NO ...

  7. "System.Web" 中不存在类型或命名空间

    System.Web”中不存在类型或命名空间名称script  /找不到System.Web.Extensions.dll引用 添加引用就行了...“添加引用→.Net→System.Web.Ente ...

  8. PKM(personal knowledge management)

    内化 一般含义 一般上,当涉及道德行为时,内化是巩固和植入某人信念.态度和价值的长期过程,而这一过程的实现牵扯到精神分析或行为方法的慎重使用. 当改变道德行为时,一组新的信念.态度和价值代替或适应于所 ...

  9. uboot环境变量初始化

    一.环境变量概述 1.环境变量的概念 可以理解为用户对软件的全局配置信息,这部分信息应该可以从永久性存储器上读取,能被查询,能被修改. 启动过程中,应该首先把环境变量读取到合适的内存区域,然后利用环境 ...

  10. MongoDB在windows服务器安装部署及远程连接MongoDB

    (.\是表示在服务器的windows powershell下需要 表示信任此命令才会执行不然会报错,自己电脑上使用时可去掉.\) 在本地使用都不需要开启权限而在服务器上需要开启安全模式所以需要在原本的 ...