1 条题解

  • 0
    @ 2025-02-04 23:03:07
    #include <bits/stdc++.h>
    using namespace std;
    typedef unsigned long long ull;
    ull T;
    struct Node{
        ull data;
        Node *next;
        Node(ull value):data(value),next(nullptr){}
    };
    struct Stack{
        Node *top;
        Stack():top(nullptr){}
    };
    bool isEmpty(const Stack &s){return s.top==nullptr;}
    void push(Stack &s,ull value){
        Node *NewNode=new Node(value);
        NewNode->next=s.top;
        s.top=NewNode;
    }
    bool pop(Stack &s,ull &poppedValue){
        if(isEmpty(s)) return false;
        else{
            Node *t=s.top;
            poppedValue=s.top->data;
            s.top=s.top->next;
            delete t;
            return true;
        }
    }
    bool top(Stack &s,ull &topValue){
        if(isEmpty(s)) return false;
        else{
            topValue=s.top->data;
            return true;
        }
    }
    ull size(Stack &s){
        Node *current=s.top;
        ull res=0;
        while(current!=nullptr){
            current=current->next;
            res++;
        }
        return res;
    }
    int main(){
        scanf("%llu",&T);
        while(T--){
            Stack s;
            ull m;
            scanf("%llu",&m);
            while(m--){
                string op;
                cin>>op;
                if(op=="push"){
                    ull x;
                    scanf("%llu",&x);
                    push(s,x);
                }else if(op=="pop"){
                    ull poppedValue;
                    if(pop(s,poppedValue)==false) puts("Empty");
                }else if(op=="query"){
                    ull topValue;
                    if(top(s,topValue)==false) puts("Anguei!");
                    else printf("%llu\n",topValue);
                }else{
                    printf("%llu\n",size(s));
                }
            }
        }
        return 0;
    }
    
  • 1

信息

ID
1000
难度
9
分类
数据结构 | 链表 点击显示
标签
(无)
递交数
7
已通过
1
通过率
14%
上传者