zoukankan      html  css  js  c++  java
  • 【模板】栈

    #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("
    %d
    ",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
    ",stack.front());
            else if(opt[0]=='e') printf("%d
    ",stack.empty());
            else if(opt[0]=='q') scanf("%d",&n),printf("%d
    ",stack.iq(n));
            else if(opt[0]=='g') break;
        }
        return 0;
    }
  • 相关阅读:
    隔离级别 && SNAPSHOT
    多态性&& 虚函数 && 抽象类
    socket编程
    [APIO2015]巴邻旁之桥
    LuoguP3701 「伪模板」主席树
    线段树标记永久化
    [HNOI2015]开店
    NOIP2017划水记
    FFTNTT总结
    [THUWC 2017]在美妙的数学王国中畅游
  • 原文地址:https://www.cnblogs.com/arcturus/p/9179969.html
Copyright © 2011-2022 走看看