定长循环队列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;
}

转载于:https://www.cnblogs.com/tyroneren/p/6250296.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值