QUEUE——队列(procedure)
来源:互联网 发布:javascript 数组初始化 编辑:程序博客网 时间:2024/06/05 18:32
#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)
- 优先级队列(priority——queue)
- 数据结构——队列(queue)
- 优先队列——二项队列(binominal queue)
- RAW-OS学习之——消息队列(queue)
- 优先队列——priority queue
- 消息队列——The message queue
- Java中的队列API——Queue
- STL—— queue 单向队列
- queue —— 队列使用示例
- C++24、stl——queue 队列
- STL练习: queue ——队列
- Java中的队列API——Queue
- STL--queue(队列)
- 队列(Queue)
- 数据结构-队列(queue)
- C# 队列(Queue)
- STL - queue(队列)
- IPsec
- 什么是NP问题,什么是NP hard问题,什么是NP完全问题。
- 安装 SSH Tunneling
- 如何发送和接收应用层数据包?
- 关于NP-hard NP-complete问题定义典故与解释证明
- QUEUE——队列(procedure)
- 二叉树遍历方法小结
- Ubuntu中Git服务器搭建
- 大话设计模式_程杰(奋斗的小鸟)_PDF 电子书
- 一个从NDK中提取独立工具链的脚本
- 轻松使用线程: 不共享有时是最好的——利用 ThreadLocal 提高可伸缩性
- 从1-n这n个数里面,随机选出若干个数,使之和为sum
- 负载类型为 MPEG-4 Elementary Stream 的 RTP 包的封包讲解
- HDOJ 4386 —— 简单几何