3335-数据结构实验之栈与队列八:栈的基本操作

来源:互联网 发布:淘宝官方店是正品吗 编辑:程序博客网 时间:2024/06/05 15:34
#include <bits/stdc++.h>using namespace std;typedef int ElemType;int m;class Stack{private:    ElemType *up;    ElemType *base;    ElemType length;public:    Stack(){        base = new ElemType;        up = base;        length = 0;    }    void push(ElemType x){        *up++ = x;        length++;    }    void pop(){        up--;        length--;    }    ElemType top(){        return *(up - 1);    }    ElemType size(){        return length;    }    bool empty(){        return 0 == length;    }};int main(){    int T,n;    cin >> T;    while(T--)    {        cin >> m >> n;        Stack Q;        while(n--)        {            char s;            int x;            cin >> s;            if(s == 'P')            {                cin >> x;                if(Q.size() >= m)                {                    cout << "F" << endl;                }                else                {                    Q.push(x);                }            }            else if(s == 'A')            {                if(Q.empty())                {                    cout << "E" << endl;                }                else                {                    cout << Q.top() << endl;                }            }            else if(s == 'O')            {                if(Q.empty())                {                    cout << "E" << endl;                }                else                {                    cout << Q.top() << endl;                    Q.pop();                }            }        }        if(T)        {            cout << endl;        }    }    return 0;}
阅读全文
0 0
原创粉丝点击