栈和循环队列

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之前也是有一个空格的

//新建一个栈,读取标准输入3个整数3 4 5,入栈3 4 5,
// 依次出栈,打印 5 4 3,
// 新建循环队列(Maxsize为5),读取标准输入3 4 5 6 7,
// 入队7时,队满,打印false,
// 然后依次出队,输出 3 4 5 6
#include <stdlib.h>
#include <stdio.h>

#define MaxSize 5
typedef int ElemType;
typedef struct {
    ElemType data[MaxSize];
    int top;
}SqStack;

void InitStack(SqStack &S)
{
    S.top = -1;//初始化栈,就是S.top = -1,让栈为空
}

bool StackEmpty(SqStack S)
{
    if(S.top == -1)
    {
        return true;
    }else{
        return false;
    }
}

//入栈
bool Push(SqStack &S,ElemType x)
{
    //判断栈是否满了
    if(S.top == MaxSize-1)
    {
        return false;
    }
    S.data[++S.top] = x;//等价于S.top = S.top+1;   S.data[S.top] = x;
    return true;
}

//获取栈顶元素
bool GetTop(SqStack S,ElemType &m)
{
    if(StackEmpty(S))
    {
        return false;
    }
    m = S.data[S.top];//拿栈顶元素
    return true;
}

//出栈
bool Pop(SqStack &S,ElemType &m)
{
    if(StackEmpty(S))
    {
        return false;
    }
    m = S.data[S.top--];//出栈
    return true;
}

//-------------------循环队列------------------------
typedef struct {
    ElemType data[MaxSize];
    int front,rear;
}SqQueue;

//新建循环队列
void InitSqQueue(SqQueue &Q)
{
    Q.front = Q.rear = 0;//初始化循环队列,就是让头和尾都指向零号
}

//判断是否为空
bool isEmpty(SqQueue Q){
    return Q.rear == Q.front;
}

//入队
bool EnQueue(SqQueue &Q,ElemType x){
    if((Q.rear+1)%MaxSize == Q.front)//判断循环队列是否满了
    {
        printf("false\n");
        return false;
    }
    Q.data[Q.rear] = x;
    Q.rear = (Q.rear+1)%MaxSize;
    return true;
}

//出队
bool DeQueue(SqQueue &Q,ElemType &x)
{
    if(Q.rear == Q.front){
        return false;
    }
    x = Q.data[Q.front];//出队
    Q.front = (Q.front+1)%MaxSize;
    return true;
}


int main() {
    SqStack S;
    ElemType a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    bool flag;
    InitStack(S);//初始化
    Push(S,a);
    Push(S,b);
    Push(S,c);
    ElemType m;
    flag = Pop(S,m);
    if(flag)
    {
        printf(" %d ",m);
    }
    flag = Pop(S,m);
    if(flag)
    {
        printf("%d ",m);
    }
    flag = Pop(S,m);
    if(flag)
    {
        printf("%d\n",m);
    }
    SqQueue Q;
    InitSqQueue(Q);
    ElemType d,e,f,g,h;
    scanf("%d %d %d %d %d",&d,&e,&f,&g,&h);
    EnQueue(Q,d);
    EnQueue(Q,e);
    EnQueue(Q,f);
    EnQueue(Q,g);
    EnQueue(Q,h);
    bool ret;
    ElemType element;
    ret = DeQueue(Q,element);
    if(ret)
    {
        printf(" %d ",element);
    }
    ret = DeQueue(Q,element);
    if(ret)
    {
        printf("%d ",element);
    }
    ret = DeQueue(Q,element);
    if(ret)
    {
        printf("%d ",element);
    }
    ret = DeQueue(Q,element);
    if(ret)
    {
        printf("%d",element);
    }
    return 0;
}

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值