题解 | #【模板】栈#

【模板】栈

https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define maxsize 100000
using namespace std;
typedef struct stack
{
    int* top;
    int* base;
}stack;
void initstack(stack &S)
{
    S.base=new int[maxsize];
    S.top=S.base;
}
void pushstack(stack &S,int e)
{
    *S.top=e;
    S.top++;
}
void popstack(stack &S)
{
    if(S.top==S.base)
    printf("error\n");
    else
    {
        S.top--;
        printf("%d\n",*S.top);
    }
}
void topstack(stack &S)
{
    if(S.top==S.base)
    printf("error\n");
    else
    {
        printf("%d\n",*(S.top-1));
    }
}
int main()
 {
    void initstack(stack &S);
    void pushstack(stack &S,int e);
    void popstack(stack &S);
    void topstack(stack &S);
    int a,b,i;
    stack s;
    initstack(s);
    char p[maxsize];
    scanf("%d",&a);
    for(i=1;i<=a;i++)
    {
        scanf("%s",p);
        if(strcmp(p,"push")==0)
        {
        scanf("%d",&b);
        pushstack(s,b);
        }
        if(strcmp(p,"pop")==0)
        popstack(s);
        if(strcmp(p,"top")==0)
        topstack(s);
    }
    return 0;
}

全部评论

相关推荐

迟缓的斜杠青年巴比Q了:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
05-20 21:57
已编辑
门头沟学院 Java
喜欢吃卤蛋的悲伤蛙在提需求:建信融通没消息吧,我2说有实习挂简历不理了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务