请你实现一个循环队列,该循环队列可利用的空间大小等于n个int型变量的大小

操作: push x:将x加入到循环队列尾端。若循环队列已满,输出full,否则不输出任何内容。保证x为int型整数。 front:输出队首元素,队首不出队。若队列为空,输出empty。 pop:输出队首元素,且队首出队。若队列为空,输出empty。

输入描述: 第一行输入两个整数n,q(1≤n,q≤10 5 ),表示循环队列可利用的空间大小和操作次数。 接下来的q行,每行一个字符串,表示一个操作。保证操作是题目描述中的一种。

输出描述: 按对应操作要求输出。

编程要求 请在右侧编辑器中完成代码,补充Begin...End之间的程序代码。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

typedef int DataType;       //元素数据类型

const int MAXN = 100001; //队列最大容量

//循环队列类型定义
typedef struct {
    DataType data[MAXN];
    int head;       //队首指针
    int tail;       //队尾指针
    int MaxSize;    //队列最大容量
} SqQueue;

//初始化一个空的循环队列:(1)设置队列最大容量,(2)设置队首、队尾指针
void InitQueue(SqQueue*& Q, int capacity) {

   //请在下面编写代码
   /**********************Begin**********************/
    Q = (Queue*)malloc(sizeof(Queue));
    Q->MaxSize = MaxSize;
    Q->head = Q->tail = 0;
   /***********************End***********************/
}

//判队列是否为空
int QueueEmpty(SqQueue* Q) {
    //请在下面编写代码
   /**********************Begin**********************/
   return Q->head == Q->tail;
   /***********************End***********************/
}

//判队列是否满
int QueueFull(SqQueue* Q) {
    //请在下面编写代码
   /**********************Begin**********************/
    return ((Q->tail + 1) % Q->MaxSize == Q->head);
   /***********************End***********************/
}

//入队列操作
void Push(SqQueue*& Q, DataType e) {
    //请在下面编写代码
   /**********************Begin**********************/
   if ((Q->tail + 1) % Q->MaxSize == Q->head) return;
     Q->data[Q->tail] = e;
	 Q->tail = (Q->tail + 1) % Q->MaxSize;
   /***********************End***********************/
}

//删除队首元素:队首元素存入变量e
void Pop(SqQueue*& Q, DataType& e) {
    //请在下面编写代码
   /**********************Begin**********************/
 if (Q->head == Q->tail) return;
	 e = Q->data[Q->head];
	 Q->head = (Q->head + 1) % Q->MaxSize;
   /***********************End***********************/
}

//取队首元素,存入变量e
void GetHead(SqQueue*& Q, DataType& e) {
    //请在下面编写代码
   /**********************Begin**********************/
	 if (Q->head == Q->tail) return;
	//   e=Q->data[(Q->head+1)% Q->MaxSize];
    e=Q->data[Q->head];   /***********************End***********************/
}


//主函数
int main() {
    int n, q;
    scanf("%d %d", &n, &q); //输入队列容量、询问次数

    SqQueue* Q;             //声明循环队列Q
    //循环队列,里面最多放置n个元素,循环队列容量为n+1
    InitQueue(Q, n + 1);

    //请在下面编写代码
   /**********************Begin**********************/


    while (q--) {
        int e;
        char op[10];
        scanf("%s", op);
        switch (op[1]) {
        case 'u':         //push
            scanf("%d", &e);
            if (FullQueue(Q)) {
                printf("full\n");
            } else {
                Push(Q, e);
            }
            break;
        case 'o':      //pop
            if (EmptyQueue(Q)) {
                printf("empty\n");
            } else {
                printf("%d\n", Pop(Q));
            }
            break;
        case 'r':      //front
            if (EmptyQueue(Q)) {
                printf("empty\n");
            } else {
                printf("%d\n", GetFront(Q));
            }
            break;
        }
    }
 
   /***********************End***********************/
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值