王道oj课时13作业(栈、队列)

aed1caa80f8141d48388266251fb44e4.png

 

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

 

---------------------------------------------------------------------------------

题解:

基础数据结构,但是要注意这是循环链表,入队出队需要对MaxSize取余,我一开始一直runtime error不知道问题在哪,后面才发现是一直+1了,没有取余

 

#include <iostream>
#define MaxSize 5

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

typedef struct
{
    int data[MaxSize];
    int top;
}Stack;

void initStack(Stack & S)
{
    S.top = -1;//后一位为第一个存储位
}

bool EmptyStack(Stack S)
{
    if(S.top==-1)//判空
    {
        return true;
    } else{
        return false;
    }
}

bool PushStack(Stack & S, int e)
{
    if(S.top==MaxSize-1)//判满
    {
        return false;
    }
    S.data[++S.top] = e;//先加一,再存入
    return true;
}

bool PopStack(Stack & S, int &e)
{
    if(EmptyStack(S) == 1)
    {
        return false;
    }else
    {
        e=S.data[S.top--];//先赋值再减一
    }
    return true;
}

void test01()
{
    Stack S;
    initStack(S);
    int x;
    int count = 3;

    int a,b,c;
    while (count--)//压栈3个元素
    {
        scanf("%d",&a);
        PushStack(S,a);
    }

    int e;
    while (!EmptyStack(S))//不空就弹栈
    {
        PopStack(S,e);
        printf("%2d",e);
    }
    printf("\n");
}

typedef struct
{
    int data[MaxSize];
    int front;//头指针
    int rear;//尾指针
}SqQueue;//牺牲一个空间判满,rear指向最后一个元素

void initSqQueue(SqQueue &S)
{
    S.front = 0;
    S.rear = -1;
}

bool EmptySqQueue(SqQueue S)
{
    if((S.rear + 1)%MaxSize == S.front)
    {
        return true;
    }else
    {
        return false;
    }
}

bool FullSqQueue(SqQueue S)
{
    if((S.rear + 2)%MaxSize == S.front)//牺牲了一个空间
    {
        return true;
    }else
    {
        return false;
    }
}

bool EnSqQueue(SqQueue &S, int e)//入队
{
    if((FullSqQueue(S))) {
        printf("false\n");
        return false;
    }else {
        S.rear = (S.rear + 1) % MaxSize;//先后移后入队
        S.data[S.rear] = e;
        return true;
    }
}

bool DeSqQueue(SqQueue &S, int &e)//出队
{
    e = S.data[S.front];//先出队后后移
    S.front = (S.front + 1)%MaxSize;

    return true;

}


void test02()
{
    SqQueue S;
    initSqQueue(S);
    int count = 5;

    int a;
    int e;
    while (count--)//入队5个元素
    {
        scanf("%d",&a);
        EnSqQueue( S, a);
    }

    while (!EmptySqQueue(S)) //不空就出队
    {
        DeSqQueue(S, e);
        printf("%2d",e);
    }
}

int main()
{

    test01();
    test02();

    return 0;
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值