循环队列操作集

防止题目太长没人看先写注意事项

杂鱼~杂鱼❤杂鱼~

1.本题入队列应先赋值后rear前移

2. 本题出队列只需要front前移

3.返回队列长度,应注意三种情况

        rear=front

        rear>=front

        rear<=front

先给出程序:

queue* createQueue()
{
    queue* Q=(queue*)malloc(sizeof(queue));
    if(Q==NULL)
        return Q;
    Q->front=0;
    Q->rear=0;
    Q->data=(int*)malloc(sizeof(int)*(MAXSIZE+1));
    return Q;
}

void enQueue(queue* q, int x)
{
    q->data[q->rear]=x;
    q->rear=(q->rear+1)%MAXSIZE;
}

bool isFull(queue* q)
{
    if((q->rear+1)%MAXSIZE==q->front)
        return true;
    return false;
}
void deQueue(queue* q)
{
    q->front=(q->front+1)%MAXSIZE;
}

int front(queue* q)
{
    int n;
    n=q->data[q->front];
    return n;
}
bool isEmpty(queue* q)
{
    if(q->front==q->rear)
        return true;
    return false;
}

int size(queue* q)
{
    int front=q->front;
    int rear=q->rear;
    if(rear<front)
    return rear+MAXSIZE-front;
    if(rear==front)
        return 0;
    return rear-front;
}

void destroyQueue(queue* q)
{
    free(q);
}

本题要求实现循环队列操作集。

函数接口定义:

在这里描述函数接口。例如:

#define MAXSIZE 10

typedef struct _queue
{
    int front;//队头指针
    int rear;//队尾指针
    int *data;//指向数据区
}queue;

//创建一个空队列
queue* createQueue();
//入队
void enQueue(queue* q, int x);
//判断队列是否已满
bool isFull(queue* q);
//出队
void deQueue(queue* q);
//得到队头元素的值
int front(queue* q);
//判断队列是否为空
bool isEmpty(queue* q);
//返回队列长度
int size(queue* q);
//销毁队列
void destroyQueue(queue* q);
//从0号位置开始输出数据区的所有元素。无效位置输出'X'
void show(queue* q);

裁判测试程序样例: 

在这里给出函数被调用进行测试的例子。例如:
/*循环队列*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define MAXSIZE 10

typedef struct _queue
{
    int front;//队头指针
    int rear;//队尾指针
    int *data;//指向数据区
}queue;

//创建一个空队列
queue* createQueue();
//入队
void enQueue(queue* q, int x);
//判断队列是否已满
bool isFull(queue* q);
//出队
void deQueue(queue* q);
//得到队头元素的值
int front(queue* q);
//判断队列是否为空
bool isEmpty(queue* q);
//返回队列长度
int size(queue* q);
//销毁队列
void destroyQueue(queue* q);
//从0号位置开始输出数据区的所有元素。无效位置输出'X'
void show(queue* q);

int main(void)
{
    char cmd[20];
    queue *pQueue = createQueue();
    int x;
    scanf("%s", cmd);
    while (strcmp(cmd, "END") != 0)
    {
        if (strcmp(cmd, "ENQUEUE") == 0)
        {
            if (isFull(pQueue) == true)
            {
                printf("FULL\n");
            }
            else
            {
                scanf("%d", &x);
                enQueue(pQueue, x);
            }
        }
        if (strcmp(cmd, "DEQUEUE") == 0)
        {
            if (isEmpty(pQueue) == true)
            {
                printf("EMPTY\n");
            }
            else
            {
                deQueue(pQueue);
            }
        }
        if (strcmp(cmd, "GETFRONT") == 0)
        {
            x = front(pQueue);
            printf("%d\n", x);
        }
        if (strcmp(cmd, "SIZE") == 0)
        {
            printf("SIZE = %d\n", size(pQueue));
        }
        if (strcmp(cmd, "SHOW") == 0)
        {
            show(pQueue);
        }
        scanf("%s", cmd);
    }
    destroyQueue(pQueue);
    return 0;
}

//从0号位置开始输出数据区的所有元素。无效位置输出'X'
void show(queue* q)
{
    if (q->front == q->rear)
        return;
    if (q->front < q->rear)
    {
        for (int i = 0; i < q->front; i++)
        {
            printf("X ");
        }
        for (int i = q->front; i < q->rear; i++)
        {
            printf("%d ", q->data[i]);
        }
        for (int i = q->rear; i < MAXSIZE; i++)
        {
            printf("X ");
        }
    }
    else
    {
        for (int i = 0; i < q->rear; i++)
        {
            printf("%d ", q->data[i]);
        }
        for (int i = q->rear; i < q->front; i++)
        {
            printf("X ");
        }
        for (int i = q->front; i < MAXSIZE; i++)
        {
            printf("%d ", q->data[i]);
        }
    }
    printf("\n");
}
/* 请在这里填写答案 */

输入样例:
在这里给出一组输入。例如:

DEQUEUE
ENQUEUE 1
ENQUEUE 2
ENQUEUE 3
SHOW
GETFRONT
SIZE
ENQUEUE 4
ENQUEUE 5
ENQUEUE 6
ENQUEUE 7
ENQUEUE 8
ENQUEUE 9
ENQUEUE 10
ENQUEUE 11
SHOW
GETFRONT
SIZE
DEQUEUE
DEQUEUE
DEQUEUE
SHOW
GETFRONT
SIZE
ENQUEUE 12
ENQUEUE 13
SHOW
GETFRONT
SIZE
END

输出样例:
在这里给出相应的输出。例如:

EMPTY
1 2 3 X X X X X X X 
1
SIZE = 3
FULL
FULL
1 2 3 4 5 6 7 8 9 X 
1
SIZE = 9
X X X 4 5 6 7 8 9 X 
4
SIZE = 6
13 X X 4 5 6 7 8 9 12 
4
SIZE = 8

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值