#include using namespace std; #include using namespace std; #define MAXQSIZE 10

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

typedef struct QNode { QElemType data; struct QNode *next; }QNode,*QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue;

Status InitQueue(LinkQueue &Q) { Q.front = Q.rear = new QNode; Q.front->next = NULL; return OK; } Status EnQueue(LinkQueue &Q, QElemType &e) { QueuePtr p = new QNode; p->data = e; p->next = NULL; Q.rear->next = p; Q.rear = p; return OK; } Status DeQueue(LinkQueue &Q, QElemType &e) { if (Q.front == Q.rear) { return ERROR; } QueuePtr p = Q.front->next; e = p->data; Q.front->next = p->next; if (Q.rear == p) { Q.rear = Q.front; } delete p; return OK; } QElemType GetHead(LinkQueue Q) { if (Q.front != Q.rear) { return Q.front->next->data; } }

Status queueTraverse(LinkQueue Q) { if (Q.front == Q.rear) { cout << "Queue Empty!" << endl; return ERROR; } QueuePtr p = Q.front->next; while (p != NULL) { cout << p->data; p = p->next; } cout << endl; return OK; }

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

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

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

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