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

队空的条件: front=rear

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

代码实现如下:



#include<stdio.h>
#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;
}
运行结果如图:

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

相关文章:

  • 2021-11-18
  • 2020-07-18
  • 2021-11-17
  • 2021-03-12
  • 2018-11-27
  • 2020-06-13
  • 2021-11-06
  • 2020-07-28
猜你喜欢
  • 2019-02-27
  • 2021-01-27
  • 2020-06-30
  • 2020-06-11
  • 2019-09-26
  • 2020-06-01
  • 2021-09-27
  • 2018-06-27
相关资源
相似解决方案