数据结构c语言循环队列代码,数据结构C语言实现----循环队列

代码如下:

#include

#include

typedef char ElemType;

#define MAXQUEUE 100

typedef struct

{

ElemType *base;

int front;

int rear;

}cycleQueue;

/

//创建一个循环队列

void initqueue(cycleQueue *q)

{

q->base = (ElemType*)malloc(sizeof(cycleQueue) * MAXQUEUE);//为循环队列申请连续空间

if (!q->base)

{

exit(0);

}

q->front = q->rear = 0;//初始换队首队尾位置为0

}

/

//入循环队列

void EnQueue(cycleQueue *q , ElemType e)

{

if ((q->rear+1) % MAXQUEUE== q->front)

{

return;

}

q->base[q->rear] = e;

q->rear = (q->rear+1)%MAXQUEUE;

}

///

//出循环列表

void DeQueue(cycleQueue *q , ElemType *e)

{

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

{

return;

}

*e = q->base[q->front];

q->front = (q->front+1)%MAXQUEUE;

}

int main()

{

cycleQueue q;

initqueue(&q);

printf("正在创建循环队列...\n创建成功!\n");

printf("请输入存入循环队列的数据:");

ElemType e;

while ((e = getchar())!=‘\n‘)

{

if (e!=‘\n‘)

{

EnQueue(&q,e);

}

}

printf("正在打印循环队列...\n当前循环队列为:");

for (size_t i = 0; i < q.rear; i++)

{

printf("%c",q.base[q.front+i]);

}

putchar(‘\n‘);

printf("请输入要从队头出队列几个元素:");

int c;

scanf("%d",&c);

while (c)

{

DeQueue(&q , &e);

printf("%c已出循环队列!\n",e);

c--;

}

printf("正在打印循环队列...\n当前循环队列为:");

for (size_t i = 0; i < q.rear; i++)

{

printf("%c",q.base[q.front+i]);

}

putchar(‘\n‘);

return 0;

}

运行结果:

1acbb147e9cde4288aae361b7f15565c.png

原文:https://www.cnblogs.com/jerryleesir/p/13340286.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值