定长循环队列C语言实现
#ifndef _CONST_H_
#define _CONST_H_
#include <stdio.h>
#include <stdlib.h>
typedef enum
{
False = 0,
True,
}Bool;
typedef int ElemType;
#define QUEUE_MAX_SIZE 10
#define STACK_INIT_SIZE 10
#define STACK_INCREMENT_SIZE 2
#define Null ((void *)0)
typedef enum
{
NORMAL = 0,
ERROR,
UNDERFLOW,
OVERFLOW,
STATUSCOUNT,
}Status;
#endif
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include "Const.h"
typedef struct queue
{
ElemType *base;
int front;
int rear;
}Queue, *pQueue;
Status InitQueue(Queue *pQ);
Bool IsQueueFull(pQueue pQ);
Bool IsQueueEmpty(pQueue pQ);
Bool EnQueue(pQueue pQ, ElemType elme);
Bool DeQueue(pQueue pQ, ElemType *e);
void DestoryQueue(pQueue pQ);
void ClearQueue(pQueue pQ);
ElemType GetHead(pQueue pQ);
int GetQueueLength(pQueue pQ);
#endif
#include "Queue.h"
Status InitQueue(Queue *pQ)
{
pQ->front = 0;
pQ->rear = 0;
pQ->base = (ElemType *)malloc(QUEUE_MAX_SIZE * sizeof(ElemType));
if (Null == pQ->base)
{
printf("Can not malloc target size memory");
return ERROR;
}
}
Bool IsQueueFull(pQueue pQ)
{
if ((pQ->rear + 1) % QUEUE_MAX_SIZE == pQ->front)
{
return True;
}
else
{
return False;
}
}
Bool IsQueueEmpty(pQueue pQ)
{
if (pQ->rear == pQ->front)
{
return True;
}
else
{
return False;
}
}
Bool EnQueue(pQueue pQ, ElemType elme)
{
if (IsQueueFull(pQ))
{
printf("The Queue Is Full.");
return False;
}
else
{
pQ->base[pQ->rear] = elme;
pQ->rear = (pQ->rear + 1) % QUEUE_MAX_SIZE;
}
}
Bool DeQueue(pQueue pQ, ElemType *e)
{
if (IsQueueEmpty(pQ))
{
printf("The Queue Is Empty.");
return False;
}
else
{
*e = pQ->base[pQ->front];
pQ->front = (pQ->front + 1) % QUEUE_MAX_SIZE;
return True;
}
}
void DestoryQueue(pQueue pQ)
{
if (pQ->base)
{
free(pQ->base);
}
pQ->rear = 0;
pQ->front = 0;
pQ->base = Null;
}
void ClearQueue(pQueue pQ)
{
pQ->rear = 0;
pQ->front = 0;
}
ElemType GetHead(pQueue pQ)
{
if (IsQueueEmpty(pQ))
{
printf("The queue is empty.");
return 0;
}
return pQ->base[pQ->front];
}
int GetQueueLength(pQueue pQ)
{
return pQ->rear - pQ->front;
}
定长循环队列C语言实现的更多相关文章
- 【数据结构】循环队列 C语言实现
"Queue.h" #include "Queue.h" #include <stdio.h> #include <stdlib.h> ...
- 不定长链表队列C语言实现
#ifndef _CONST_H_#define _CONST_H_ #include <stdio.h>#include <stdlib.h> typedef enum { ...
- Redis 定长队列的探索和实践
vivo 互联网服务器团队 - Wang Zhi 一.业务背景 从技术的角度来说,技术方案的选型都是受限于实际的业务场景,都以解决实际业务场景为目标. 在我们的实际业务场景中,需要以游戏的维度收集和上 ...
- 数据结构算法C语言实现(十二)--- 3.4循环队列&队列的顺序表示和实现
一.简述 空队列的处理方法:1.另设一个标志位以区别队列是空还是满:2.少用一个元素空间,约定以队列头指针在队尾指针下一位置上作为队列呈满的状态的标志. 二.头文件 //3_4_part1.h /** ...
- C语言数据结构-循环队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作
1.数据结构-循环队列的实现-C语言 #define MAXSIZE 100 //循环队列的存储结构 typedef struct { int* base; //基地址 int _front; //头 ...
- C语言实现使用动态数组实现循环队列
我在上一篇博客<C语言实现使用静态数组实现循环队列>中实现了使用静态数组来模拟队列的操作. 因为数组的大小已经被指定.无法动态的扩展. 所以在这篇博客中,我换成动态数组来实现. 动态数组能 ...
- 快学Scala 第三课 (定长数组,变长数组, 数组循环, 数组转换, 数组常用操作)
定长数组定义: val ar = new Array[Int](10) val arr = Array("aa", "bb") 定长数组赋值: arr(0) = ...
- 数据结构(C语言版)---第三章栈和队列 3.4.2 队列的链式表示和实现(循环队列)
这个是循环队列的实现,至于串及数组这两章,等有空再看,下面将学习树. 源码如下: #include <stdio.h> #include <stdlib.h> #define ...
- c语言编程之循环队列
利用链表实现的循环队列,完成了队列的入队和出队,对于队空和队满用了一个flag进行标记.入队flag++,出队flag-- #include"stdio.h" typedef in ...
随机推荐
- Android下Cocos2d创建HelloWorld工程
最近在搭建Cocos2d的环境,结果各种问题,两人弄了一天才能搞好一个环境-! -_-!! 避免大家也可能会遇到我这种情况,所以写一个随笔,让大家也了解下如何搭建吧- 1.环境安装准备 下载 tadp ...
- 关于 update别名 与update select
正确写法: update 别名 set 别名点字段 =xxxx UPDATE a SET a.StandardID = (SELECT b.StandardID FROM SurgeryMappi ...
- java.util.ConcurrentModificationException --map
key:3-key key:/v1.02-key Exception in thread "main" java.util.ConcurrentModificationExcept ...
- Ubuntu Dev Box Setup
Editor VIM Sublime Atom Visual Studio Code SSH Client PAC Manager File Manager Double Commander Imag ...
- react lazyload
思路: DOM加载时,<img> 标签里,添加data-src路径 = src 路径, src路径 = 本地默认图片路径, DOM加载完成后,监听页面可视区域,有data-src时,就将s ...
- Spark:Join相关优化文章
http://blog.csdn.net/lsshlsw/article/details/48975771 https://www.douban.com/note/499691663/ http:// ...
- c语言二维数组求最大值
#include<stdio.h> int main() { ,colum=,max; ][]={{,,,},{,,,},{-,,-,}}; max=a[][]; ;i<=;i++) ...
- iis发布网站怎么支持.json文件
- Hybrid App开发者一定不要错过的框架和工具///////////z
ionicFramework 我是hybrid app的忠实粉丝和大力倡导者,从 新浪移动云开始就不断的寻找能帮助Web程序员开发出漂亮又好用的UI层框架.在历经了jqmobile.sencha to ...
- 2.MongoDB数据库简介
1).简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. mongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...