#include using namespace std; #define MAXQSIZE 10

#define OK 1 #define ERROR 0 typedef int Status; typedef char QElemType;

typedef struct { QElemType *base; int front; int rear; }SqQueue;

Status InitQueue(SqQueue &Q) { Q.base = new QElemType[MAXQSIZE]; if (!Q.base) { return ERROR; } Q.front = Q.rear = 0; return OK; } Status EnQueue(SqQueue &Q, QElemType &e) { if ((Q.rear + 1) % MAXQSIZE == Q.front) { return ERROR; } Q.base[Q.rear] = e; Q.rear = (Q.rear + 1) % MAXQSIZE; return OK; } Status DeQueue(SqQueue &Q, QElemType &e) { if (Q.front == Q.rear) { return ERROR; } e = Q.base[Q.front]; Q.front = (Q.front + 1) % MAXQSIZE; return OK; } QElemType GetHead(SqQueue Q) { if (Q.front != Q.rear) { return Q.base[Q.front]; } } Status queueTraverse(SqQueue Q) { if (Q.front == Q.rear) { cout << "Queue Empty!" << endl; return ERROR; } int i = Q.front; while (i != Q.rear) { cout << Q.base[i] << " "; i = (i + 1) % MAXQSIZE; } cout << endl; return OK; }

int main() { SqQueue Q; InitQueue(Q); int n; cin >> n; char op, e; for (int i = 0; i < n; i++) { cin >> op; if (op == '0') { DeQueue(Q, e); } else if (op == '1') { cin >> e; EnQueue(Q, e); } } queueTraverse(Q); return 0;

本关任务:实现循环队列的基本操作:初始化、入队、出队、遍历等并使用这些操作。队列中元素是大写或小写字母。输入输出输入格式:两行第一行是一个正整数n表示出队和入队的次数第二行是n次出队和入队操作序列其中1表示入队0表示出队如果是入队则1后跟一个大写或小写字母。输出格式:遍历队列内容从队尾到队首输出所有字母如果队为空则输出Queue Empty!输入输出样例:输入51f1e1d00输出:e#inclu

原文地址: https://www.cveoy.top/t/topic/h2Ik 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录