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;
    }
  • 相关阅读:
    session_id 生成原理
    压缩后的数据 要经过 base64_encode 后才能在网络上传送
    MySQL ANALYZE TABLE
    mysql 优化2
    mysql 查询优化
    第归调用
    『GoLang』函数
    『GoLang』控制结构
    『GoLang』语法基础
    『Python』装饰器
  • 原文地址:https://www.cnblogs.com/arcturus/p/9179969.html
Copyright © 2011-2022 走看看