zoukankan      html  css  js  c++  java
  • STL之stack

    描述

    使用STL中的stack,完成入栈、出栈、栈清空等基本操作。

    部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

     

    int main()
    {
        stack<int> st;
        int n;
        cin>>n;
        while(n--)
        {
            Op(st);
        }
        while(!st.empty())
        {
            cout<<st.top()<<endl;
            st.pop();
        }
        return 0;
    }

    输入

    输入数据第一行为整数n,接下来有n个命令,其中命令格式:

    (1)压栈:push x

    (2)出栈:pop

    (3)清空:clear

    如果部分操作无效,该操作不执行。

    输出

    执行一系列操作后,输出从栈顶到栈底的所有元素值,每行一个。

    样例输入

     5
    push 1
    push 2
    pop
    push 3
    push 4

    样例输出

    4
    3
    1

    #include <iostream>
    #include <string>
    #include <stack>
    using namespace std;
    void Op(stack<int> &st)
    {
        string ss;
        cin>>ss;
        if(ss=="push")
        {
            int n;
            cin>>n;
            st.push(n);
        }
        else if(ss=="pop")
        {
            if(!st.empty())
            {
                st.pop();
            }
        }
        else if(ss=="clear")
        {
            while(!st.empty())
            {
                st.pop();
            }
        }
        else
        {
            if(!st.empty())
            {
                cout<<st.top()<<endl;
            }
        }
    }
    int main()
    {
        stack<int> st;
        int n;
        cin>>n;
        while(n--)
        {
            Op(st);
        }
        while(!st.empty())
        {
            cout<<st.top()<<endl;
            st.pop();
        }
        return 0;
    }

     

     

  • 相关阅读:
    node 读取文件
    jQuery全局事件处理函数
    可以发送不同源请求的方式
    ajax 高度封装的函数
    jQuery中AJAX的回调
    jQuery中对AJAX的封装
    ajax 基本的封装
    AJAX 返回数据问题
    ajax 关于响应类型
    动态渲染数据到表格中
  • 原文地址:https://www.cnblogs.com/andrew3/p/8722148.html
Copyright © 2011-2022 走看看