循环顺序队列c语言实现,顺序循环队列的实现(c语言)

队列的顺序存储结构——循环队列

队空的条件: front=rear

队满的条件是: (rear+1)%QueueSize=front

代码实现如下:

#include

#define MAXSIZE 100  //假定预设分配的队列空间最多能存放100个表目

typedef char datatype;//假定队列 的 表目为 字符类型

typedef struct

{

datatype Q[MAXSIZE];

int front,rear;//对头和队尾

} SeqQuene;

SeqQuene QU;

datatype x;

void EnQueue(SeqQuene *sq,datatype x);//队列的插入

datatype DeQueue(SeqQuene *sq);//队列的删除

datatype getFront(SeqQuene *s);//获取队头元素

void clearQueue(SeqQuene *sq);//置空对列

int QueueEmpty(SeqQuene *sq);//判断队是否为空!

int main()

{

clearQueue(&QU);

printf("please input your  data:");

scanf("%c",&x);

EnQueue(&QU,x);

if(QueueEmpty(&QU))

{

printf("empty!\n");

}

else

{

printf("not empty!\n");

}

x=getFront(&QU);

printf("the moment x is %c\n",x);

x=DeQueue(&QU);

if(QueueEmpty(&QU))

{

printf("empty!\n");

}

else

{

printf("not empty!\n");

}

printf("the moment x is %c\n",x);

return 0;

}

void EnQueue(SeqQuene *sq,datatype x)

{

if(sq->front==(sq->rear+1)%MAXSIZE)

{

printf("overflow!");

}

else

{

sq->Q[sq->rear]=x;

sq->rear=(sq->rear+1)%MAXSIZE;

}

}

datatype DeQueue(SeqQuene *sq)

{

if(sq->front==sq->rear)

{

printf("underFlow");

}

else

{

sq->front=(sq->front+1)%MAXSIZE;

return sq->Q[sq->front-1];

}

}

datatype getFront(SeqQuene *sq)

{

if(sq->front==sq->rear)

{

printf("underFlow!");

}

else

{

return sq->Q[sq->front];

}

}

void clearQueue(SeqQuene *sq)

{

sq->front=sq->rear=0;

}

int QueueEmpty(SeqQuene *sq)

{

printf("the moment front is:%d,the moment rear is:%d\n",sq->front,sq->rear);

if(sq->front==sq->rear)

return 1;

else return 0;

}

运行结果如图:

96730bb9efe13ea4cf395ac36da64639.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值