QUEUE——队列(procedure)
#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);
}
最新文章
- html嵌入样式表
- http之Session&;Cookie
- myeclipse项目编码方式彻底设置
- 烂泥:学习ubuntu远程桌面(二):远程桌面会话管理
- Kettle简介
- C++中,用类和重载运算符写高精模板
- PWN! 第一次测试答案及讲解
- Git命令参考手册
- jQuery-jqprint.js打印插件使用高版本jQuery时问题
- hive理论
- Unity4.6证书激活问题
- Android设备广告投放解决方案——大量网络图片、多个网络视频的轮播、缓存与更新
- 【转载】webstorm-前端javascript开发神器中文教程和技巧分享
- Hadoop slaves 没有nodeManager
- PAT1071. Speech Patterns (25)
- java Date中方法toLocaleString过时的替代方案
- python基础的一些知识点
- Eclipse闪退解决方案
- 从TensorFlow0.12升级到TensorFlow1.13
- selenium之坑:点击后页面刷新重新获取刷新前的页面(StaleElementReferenceException:Message:Element not found in the cache...)
热门文章
- wireshark添加用户执行
- ZOJ 1733 Common Subsequence(LCS)
- Mysql创建删除索引
- 大数据技术人年度盛事! BDTC 2016将于12月8-10日在京举行
- 求LR(0)文法的规范族集和ACTION表、GOTO表的构造算法
- C#(asp.net)备份还原mssql数据库代码【转】
- 拦截QT关闭窗口的CloseEvent
- 自己挖坑自己跳 之JsonMappingException: (was java.lang.NullPointerException) (through reference chain:)
- Linux下Java 编译运行说明
- websphere性能设置和日常维护