Description
新建一个栈,读取标准输入3个整数3 4 5,入栈3 4 5,依次出栈,打印 5 4 3,新建循环队列(Maxsize为5),读取标准输入3 4 5 6 7,入队7时,队满,打印false,然后依次出队,输出 3 4 5 6
Input
读取标准输入,内容依次是3 4 5,换行后,接着是3 4 5 6 7
Output
如果输入是3 4 5,换行,接着是3 4 5 6 7,那么输出是
5 4 3
false
3 4 5 6
注意每个数字占用两个字符的位置,5之前是有一个空格的,第三行的3之前也是有一个空格的
Sample Input 1
3 4 5
3 4 5 6 7
Sample Output 1
5 4 3
false
3 4 5 6
Sample Input 2
1 3 9
1 3 5 7 9
Sample Output 2
9 3 1
false
1 3 5 7
题解
#include<stdio.h>
#include<stdlib.h>
#define maxSize 5
typedef struct stack{
int data[maxSize];
int top;
}SqStack;
typedef struct queue{
int data[maxSize];
int front,rear;
}circleQueue;
void initQueue(circleQueue &q){
q.front = q.rear = 0;
}
void initStack(SqStack &s){
s.top = -1;
}
bool isStackEmpty(SqStack s){
return s.top == -1;
}
bool isQueueEmpty(circleQueue q){
return q.rear == q.front;
}
bool isStackFull(SqStack s){
return maxSize - 1 == s.top;
}
bool isQueueFull(circleQueue q){
return (q.rear+1)%maxSize == q.front;
}
bool enQueue(circleQueue &q,int x){
if(isQueueFull(q)) return false;
q.data[q.rear] = x;
q.rear = (q.rear+1)%maxSize;
return true;
}
bool push(SqStack &s,int x){
if(isStackFull(s)) return false;
s.data[++s.top] = x;
return true;
}
bool deQueue(circleQueue &q){
if(isQueueEmpty(q)) return false;
printf("%2d",q.data[q.front]);
q.front = (q.front+1)%maxSize;
if(isQueueEmpty(q)){
printf("\n");
return true;
}
return false;
}
bool pop(SqStack &s){
if(isStackEmpty(s)) return false;
printf("%2d",s.data[s.top--]);
if(isStackEmpty(s)){
printf("\n");
return true;
}
return false;
}
int main(){
SqStack s;
initStack(s);
int x;
for(int i=0;i<3;i++){
scanf("%d",&x);
push(s,x);
}
while(!pop(s));
circleQueue q;
initQueue(q);
for(int i=0;i<5;i++){
scanf("%d",&x);
if(!enQueue(q,x)) printf("false\n");
}
while(!deQueue(q));
return 0;
}