队列是一种操作受限的线性表,具有先进先出的特性。现在有如下操作。
1:取队头元素
2:入队
3:出队
请给出缺失的函数(用课堂上介绍的循环队列实现)。
输入要求:
多组输入
每组输入一个整数cmd,代表操作命令,如果是入队操作,再输入一个需要入队的整数。
输出要求:
根据输入输出操作结果,详见代码
数据示例1:
输入:
121222324252613126333333
输出:
ERROR
OK
OK
OK
OK
OK
ERROR
1
OK
2
OK
OK
OK
OK
OK
OK
ERROR
#include<stdio.h>#include<stdlib.h>#defineMAXSIZE5// 函数结果状态码 typedefint Status;#defineOK1#defineERROR0#defineOVERFLOW-2typedefint ElemType;typedefstruct{
ElemType *base;int front;int rear;int size;}SqQueue;// 初始化空队列,数组长度为 MAXSIZE voidinit(SqQueue &Q);// 将队头元素赋值到e中。成功返回OK,否则返回ERROR
Status front(SqQueue Q,int&e);// 将e入队。成功返回OK,否则返回ERROR
Status enqueue(SqQueue &Q, ElemType e);// 出队。成功返回OK,否则返回ERROR
Status dequeue(SqQueue &Q);intmain(){int cmd, e, re;
SqQueue Q;init(Q);while(~scanf("%d",&cmd)){switch(cmd){case1:// get front
re =front(Q, e);if(re)printf("%d\n", e);elseprintf("ERROR\n");break;case2:// enqueue scanf("%d",&e);
re =enqueue(Q,e);if(re)printf("OK\n");elseprintf("ERROR\n");break;case3:// dequeue
re =dequeue(Q);if(re)printf("OK\n");elseprintf("ERROR\n");break;default:printf("Unknown Command\n");break;}}}//// 初始化空队列,数组长度为 MAXSIZE voidinit(SqQueue &Q){
Q.base=new ElemType[MAXSIZE];
Q.front=1;
Q.rear=0;
Q.size=0;}// 将队头元素赋值到e中。成功返回OK,否则返回ERROR
Status front(SqQueue Q,int&e){if(Q.size==0)return ERROR;
e=Q.base[Q.front];return OK;}// 将e入队。成功返回OK,否则返回ERROR
Status enqueue(SqQueue &Q, ElemType e){if(Q.size==MAXSIZE)return ERROR;if(++Q.rear==MAXSIZE) Q.rear=0;
Q.base[Q.rear]=e;
Q.size++;return OK;}// 出队。成功返回OK,否则返回ERROR
Status dequeue(SqQueue &Q){if(Q.size==0)return ERROR;if(++Q.front==MAXSIZE)
Q.front=0;
Q.size--;return OK;}
0802
队列是一种操作受限的线性表,具有先进先出的特性。现在有如下操作。
1:取队头元素
2:入队
3:出队
请给出缺失的函数(用链队列实现)。
输入要求:
多组输入
每组输入一个整数cmd,代表操作命令,如果是入队操作,再输入一个需要入队的整数。
输出要求:
根据输入输出操作结果,详见代码
数据示例1:
输入:
1212223242526131263133313131
输出:
ERROR
OK
OK
OK
OK
OK
OK
1
OK
2
OK
OK
3
OK
OK
OK
6
OK
6
OK
ERROR
#include<stdio.h>#include<stdlib.h>// 函数结果状态码 typedefint Status;#defineOK1#defineERROR0#defineOVERFLOW-2typedefint ElemType;typedefstructQNode{
ElemType data;//数据域structQNode*next;//指针域}QNode;typedefstruct{
QNode *front;// 队头
QNode *rear;// 队尾 }LinkQueue;// 初始化空队列 voidinit(LinkQueue &Q);// 将队头元素赋值到e中。成功返回OK,否则返回ERROR
Status front(LinkQueue Q,int&e);// 将e入队,返回OK
Status enqueue(LinkQueue &Q, ElemType e);// 出队。成功返回OK,否则返回ERROR
Status dequeue(LinkQueue &Q);intmain(){int cmd, e, re;
LinkQueue Q;init(Q);while(~scanf("%d",&cmd)){switch(cmd){case1:// get front
re =front(Q, e);if(re)printf("%d\n", e);elseprintf("ERROR\n");break;case2:// enqueue scanf("%d",&e);
re =enqueue(Q,e);if(re)printf("OK\n");elseprintf("ERROR\n");break;case3:// dequeue
re =dequeue(Q);if(re)printf("OK\n");elseprintf("ERROR\n");break;default:printf("Unknown Command\n");break;}}}// 初始化空队列 voidinit(LinkQueue &Q){
Q.front=Q.rear=new QNode;
Q.front->next=NULL;}// 将队头元素赋值到e中。成功返回OK,否则返回ERROR
Status front(LinkQueue Q,int&e){if(Q.front->next==NULL)return ERROR;
e=Q.front->next->data;return OK;}// 将e入队,返回OK
Status enqueue(LinkQueue &Q, ElemType e){
QNode *p=new QNode;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;return OK;}// 出队。成功返回OK,否则返回ERROR
Status dequeue(LinkQueue &Q){if(Q.front->next==NULL)return ERROR;
QNode *p=Q.front->next;
Q.front->next=p->next;if(Q.rear==p)
Q.rear=Q.front;
delete p;return OK;}
0803
判断一个表达式中的圆括号()和方括号[]是否匹配。
输入要求:
多组输入,每组输入一个长度不大于20的字符串(无空格),占一行。
输出要求:
括号匹配输出yes,否则输出no。每组输出后换行
数据示例1:
输入:
([](x+y))[([][])][(])([())
输出:
yes
yes
no
no