6-3 循环队列操作集 (30分)_数据结构实验3_羊卓的杨

6-3 循环队列操作集 (30分)

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

函数接口定义:

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

#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

答案样例:

//创建一个空队列
queue* createQueue() {
	queue* q = (queue*)malloc(sizeof(queue)); 
	q->data = (int*)malloc(MAXSIZE*sizeof(int));
	q->front = 0;
	q->rear = 0;
	return q;
}
//入队
void enQueue(queue* q, int x) {
	q->data[q->rear] = x;
	q->rear = (q->rear+1) % MAXSIZE;
	return;
}
//判断队列是否已满
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;
	return;
}
//得到队头元素的值
int front(queue* q) {
	return q->data[q->front];
}
//判断队列是否为空
bool isEmpty(queue* q) {
	if(q->front == q->rear)
		return true;
	return false;
}
//返回队列长度
int size(queue* q) {
	int queue_size = (q->rear-q->front+MAXSIZE)%MAXSIZE;
	return queue_size;
}
//销毁队列
void destroyQueue(queue* q) {
	q->front = q->rear = 0;
}

我的好伙计你点个赞再走,你要是三连一下就更好了❀

bilibili:羊卓的杨
公众号:羊卓的杨

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值