题解 | #【模板】队列#

【模板】队列

https://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549?tpId=308&tqId=2110348&ru=/exam/oj&qru=/ta/algorithm-start/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D308

#include <iostream>
using namespace std;
#include<algorithm>
#include <string>
class stack{
    private:    
        int s[100000];
        int top=-1;
    public:
        void push(int x)
        {
            top++;
            s[top]=x;
        }
        void pop()
        {
            // if(top>=0)
            // {
            //     if(top==0)
            //     {
            //         cout<<s[0]<<endl;
            //         top=-1;
            //     }
            //     else
            //     {
            //         cout<<s[0]<<endl;
            //         reverse(s, s+top); //反转
            //         top--;
            //         reverse(s, s+top); //反转    
            //     }
            // }
            if(top>=0)
            {
                cout<<s[0]<<endl;
                for(int i=0;i<top;i++)
                {
                    s[i]=s[i+1];
                }
                    top--;
            }
            else cout<<"error"<<endl;           
        }
        void front()
        {
            if(top>=0)
            {
                cout<<s[0]<<endl;
            }
            else cout<<"error"<<endl;
        }
};

int main() {
    // int a, b;
    // while (cin >> a >> b) { // 注意 while 处理多个 case
    //     cout << a + b << endl;
    // }
    stack s;
    int n=0;
    cin >>n;
    for(int i=0;i<n;i++)
    {
        string op;
        cin>>op;
        if(op=="push")
        {
            int a=0;
            cin>>a;
            s.push(a);
        }
        if(op=="pop")
        {
            s.pop();
        }
        if(op=="front")
        {
            s.front();
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

认真搞学习:28小登的建议,投算法岗不要写什么物理竞赛,互联网+,多写点项目,用什么算法做了什么。还有本科算法是不可能的开发你这个也没有项目啊
点赞 评论 收藏
分享
06-20 17:42
东华大学 Java
凉风落木楚山秋:要是在2015,你这简历还可以月入十万,可惜现在是2025,已经跟不上版本了
我的简历长这样
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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