环形队列各种基本算法的实现及示例

#include<stdio.h>
#include<malloc.h>
#define MaxSize 5
typedef char ElemType;
typedef struct
{
 ElemType data[MaxSize];
 int front,rear;
}SqQueue;
void InitQueue(SqQueue *&q)
{
 q=(SqQueue *)malloc(sizeof(SqQueue));
 q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q)
{
 free(q);
}
bool QueueEmpty(SqQueue *p)
{
 return (p->front==p->rear);
}
bool enQueue(SqQueue *&q,ElemType e)
{
 if((q->rear+1)%MaxSize==q->front)
  return false;
 q->rear=(q->rear+1)%MaxSize;
 q->data[q->rear]=e;
 return true;
}
bool deSqQueue(SqQueue *&p,ElemType &e)
{
 if(p->front==p->rear)
  return false;
 p->front=(p->front+1)%MaxSize;
 e=p->data[p->front];
 return true;
}
int main()
{
 SqQueue *q;
 ElemType e;
 printf("环形队列的基本运算如下:\n");
 printf("(2)初始化队列q\n");
 InitQueue(q);
 printf("(2)依次进队列元素a,b,c\n");
 if(!enQueue(q,'a'))
  printf("\t提示:队满,不能进队\n"); 
 if(!enQueue(q,'b'))
  printf("\t提示:队满,不能进队\n");
 if(!enQueue(q,'c'))
  printf("\t提示:队满,不能进队\n");
 printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空"));
 if(deSqQueue(q,e)==0)
  printf("队空,不能出列\n");
 else
  printf("(4)出队一个元素%c\n",e);
 printf("(5)依次进队列的元素d,e,f\n");
  if(!enQueue(q,'d'))
 printf("\t提示:队满,不能进队\n");
  if(!enQueue(q,'e'))
 printf("\t提示:队满,不能进队\n");
  if(!enQueue(q,'f'))
 printf("\t提示:队满,不能进队\n");
 printf("(6)出队列序列:");
 while(!QueueEmpty(q))
 {
  deSqQueue(q,e);
  printf("%c",e);
 }  
 printf("\n");
 printf("(7)释放队列\n");
 DestroyQueue(q);
 return 0;
}

程序运行结果如下:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值