#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语言实现的更多相关文章

  1. 【数据结构】循环队列 C语言实现

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

  2. 不定长链表队列C语言实现

    #ifndef _CONST_H_#define _CONST_H_ #include <stdio.h>#include <stdlib.h> typedef enum { ...

  3. Redis 定长队列的探索和实践

    vivo 互联网服务器团队 - Wang Zhi 一.业务背景 从技术的角度来说,技术方案的选型都是受限于实际的业务场景,都以解决实际业务场景为目标. 在我们的实际业务场景中,需要以游戏的维度收集和上 ...

  4. 数据结构算法C语言实现(十二)--- 3.4循环队列&队列的顺序表示和实现

    一.简述 空队列的处理方法:1.另设一个标志位以区别队列是空还是满:2.少用一个元素空间,约定以队列头指针在队尾指针下一位置上作为队列呈满的状态的标志. 二.头文件 //3_4_part1.h /** ...

  5. C语言数据结构-循环队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作

    1.数据结构-循环队列的实现-C语言 #define MAXSIZE 100 //循环队列的存储结构 typedef struct { int* base; //基地址 int _front; //头 ...

  6. C语言实现使用动态数组实现循环队列

    我在上一篇博客<C语言实现使用静态数组实现循环队列>中实现了使用静态数组来模拟队列的操作. 因为数组的大小已经被指定.无法动态的扩展. 所以在这篇博客中,我换成动态数组来实现. 动态数组能 ...

  7. 快学Scala 第三课 (定长数组,变长数组, 数组循环, 数组转换, 数组常用操作)

    定长数组定义: val ar = new Array[Int](10) val arr = Array("aa", "bb") 定长数组赋值: arr(0) = ...

  8. 数据结构(C语言版)---第三章栈和队列 3.4.2 队列的链式表示和实现(循环队列)

    这个是循环队列的实现,至于串及数组这两章,等有空再看,下面将学习树. 源码如下: #include <stdio.h> #include <stdlib.h> #define ...

  9. c语言编程之循环队列

    利用链表实现的循环队列,完成了队列的入队和出队,对于队空和队满用了一个flag进行标记.入队flag++,出队flag-- #include"stdio.h" typedef in ...

随机推荐

  1. [SharePoint 2013] Automatic deployment script

    Implement automatic deployment through windows task. Add-PsSnapin Microsoft.SharePoint.PowerShell $t ...

  2. MQTT——java简单测试(二)

    服务端代码: package bsit.mqtt.demo.one_way; import org.eclipse.paho.client.mqttv3.MqttClient; import org. ...

  3. 项目 XXX 的 NuGet 程序包还原失败:找不到“xxx”版本的程序包“xxx”

    项目 XXX 的 NuGet 程序包还原失败:找不到“xxx”版本的程序包“xxx” 编译新下载的代码出错 修改包管理器的源为 http://www.nuget.org/api/v2/ .重试后成功 ...

  4. DataList 用法详解

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataList.aspx. ...

  5. 《 spring mvc 》学习计划

    第一章:1月1日-1月2日 第二章:1月3日 第三章:1月4日 第四章:1月5日 第五章:1月6日-1月7日 第六章:1月8日 第七章:1月9日 第八章:1月10日 第九章:1月11日-1月12日 第 ...

  6. 《30天自制操作系统》19_day_学习笔记

    harib16a: 这一部分,我们在系统中实现读取文件内容的命令type.在windows中,输入“type 文件名”,在Linux中,输入“cat 文件名”都可以显示文件的内容.我们先来看看如何读取 ...

  7. linux scp 远程复制文件

    1.从本机复制文件到远程scp 文件名 远程计算机用户名@远程计算机的ip:远程计算机存放该文件的路径2.从远程复制文件到本机:scp 远程计算机用户名@远程计算机ip:文件名 存放该文件的本机路径3 ...

  8. Android 开发基础及环境配置

    2011年买了第一部安卓操作系统的手机,当时势头正盛的HTC不可思议(incredible),当时的想法就是想学习下智能手机开发,但是由于各种原因,客观上是公司的项目太忙了,忙于项目管理.团队建设.客 ...

  9. 什么是Cookie对象,Session对象,Application对象。

    Cookie是: 一个由网页服务器放在您硬盘上的非常小的文本文件. 它本质上就像您的身份证明一样,并且不能像代码那样被执行或被用来散布病毒.它只能被您使用并且只能由提供的服务器读取. 使用Cookie ...

  10. mysql中使用 where 1=1和 0=1 的作用

    操作mysql的时候,经常使用where语句进行查询.当where语句不存在的时候,经常在后面加一个where 1=1 where 1=1; 这个条件始终为True,在不定数量查询条件情况下,1=1可 ...