Maximum Element——stack

Maximum Element

You have an empty sequence, and you will be given queries. Each query is one of these three types:

1 x  -Push the element x into the stack.

2    -Delete the element present at the top of the stack.

3    -Print the maximum element in the stack.

 

Input Format

The first line of input contains an integer, . The next lines each contain an above mentioned query. (It is guaranteed that each query is valid.)

Constraints 

Output Format

For each type query, print the maximum element in the stack on a new line.

Sample Input

10

1 97

2

1 20

2

1 26

1 20

2

3

1 91

3

Sample Output

26

91

#include <algorithm>

#include <iostream>

#include <stack>

#include <vector>

using namespace std;

int main()

{

    stack<int>v;

    int n;

    cin>>n;

    while(n--)

    {

        int ch;

        cin>>ch;

        if(ch==1)

        {

            int x;

            cin>>x;

            v.push(max(x,v.size()>0?v.top():x));

            cout<<v.top()<<endl;

            cout<<max(x,v.size()>0?v.top():x)<<endl;

            //stack容器的栈顶元素的读取函数为top 函数 将取出最后入栈的元素

            //如果没有元素 输入元素

            //如果有元素 比较输入元素与原来元素的大小 存大的

        }

        else if(ch==2)

        {

            v.pop();

        }

        else if(ch==3)

        {

            cout<<v.top()<<endl;

        }

    }

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值