【模板】栈

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct data{
    int stack[10010];//建栈
    int top;
    bool ins[10010];//标记是否在栈中
    void print()//输出函数
    {
        for(int i=1;i<=top;i++)
        printf("%d ",stack[i]);
        printf("\n%d\n",top);
        return ;
    }
    void push(int x)//入栈
    {
        ins[x]=true;
        stack[++top]=x;
//        this->print();
        return ;
    }
    void pop()//弹栈
    {
        if(top==0)
        {
            printf("empty");
            return ;
        }
        ins[stack[top]]=false;
        top--;
//        this->print();
        return ;
    }
    int front()//查询栈顶
    {
        return stack[top];
    }
    bool empty()//查询是否为空
    {
        if(top>0) return 0;
        else return 1;
    }
    bool iq(int x)//询问X是否在栈内
    {
        if(ins[x]==true) return 1;
        else return 0;
    }
}stack;
int n;
char opt[100];
int main()
{
    while(1)
    {
        scanf("%s",opt);
        if(opt[0]=='o') stack.pop();
        else if(opt[0]=='p') scanf("%d",&n),stack.push(n);
        else if(opt[0]=='f') printf("%d\n",stack.front());
        else if(opt[0]=='e') printf("%d\n",stack.empty());
        else if(opt[0]=='q') scanf("%d",&n),printf("%d\n",stack.iq(n));
        else if(opt[0]=='g') break;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/cn-suqingnian/p/9179969.html