1650: 队列-初始化、出队列、计算长度及打印2(顺序存储)
金币值:2
定数:8
时间限制:1.000 s
内存限制:128 M
正确:36
提交:98
正确率:36.73% 命题人:
题目描述
完成顺序循环队列的初始化、入队、出队和遍历等操作。
#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MAXSIZE 100 /* 存储空间初始分配量 */ typedef int Status; typedef int QElemType; /* QElemType类型根据实际情况而定,这里假设为int */ /* 循环队列的顺序存储结构 */ typedef struct { QElemType *base; int front; /* 头指针 */ int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */ } SqQueue; /* 初始化一个空队列Q */ Status InitQueue(SqQueue &Q); /* 返回Q的元素个数,也就是队列的当前长度 */ int QueueLength(SqQueue Q); /* 若队列未满,则插入元素e为Q新的队尾元素 */ Status EnQueue(SqQueue &Q,QElemType e); Status DeQueue(SqQueue &Q,QElemType &e); /* 从队头到队尾依次对队列Q中每个元素输出 */ void QueueTraverse(SqQueue Q); int main(void) { QElemType d; SqQueue Q; InitQueue(Q); scanf("%d",&d); while(d!=-1){ EnQueue(Q,d); scanf("%d",&d); } QueueTraverse(Q); printf("\n"); DeQueue(Q,d); printf("%d\n",d); printf("%d\n",QueueLength(Q)); QueueTraverse(Q); return 0; } /*以下为你的代码*/ Status InitQueue(SqQueue &Q){ } int QueueLength(SqQueue Q){ } Status EnQueue(SqQueue &Q,QElemType e){ } Status DeQueue(SqQueue &Q,QElemType &e){ } void QueueTraverse(SqQueue Q){ }
输入格式
在一行输入若干个整型数,以-1作为结束(-1不计入总数)
输出格式
第1行:队列中数据(从头到尾)【若存在】
第2行:出队的元素值
第3行:当前队列长度
第4行:当前队列中数据(从头到尾)【若存在】
第2行:出队的元素值
第3行:当前队列长度
第4行:当前队列中数据(从头到尾)【若存在】
输入样例 复制
1 2 3 4 5 -1
输出样例 复制
1 2 3 4 5
1
4
2 3 4 5