Program to construct Maximum Stack with given operations in C++

C++Server Side ProgrammingProgramming

Suppose we want to make a maximum stack, which supports following operations −

  • MaxStk() this will construct a new instance of a maximum stack

  • push(val) inserts val to the stack

  • top() get the top most element from stack

  • max() get the maximum element from the stack

  • pop() removes and returns the top most element from the stack

  • popmax() removes and returns the maximum element from the stack

Now construct the maximum stack by calling MasStk(), then push three values like 5, 15, 10, then call top(), max(), popmax(), max() pop(), top() functions respectively. then the initial stack status will be [5, 15, 10], and corresponding output for the functions: 10, 15, 15, 10, 10, 5

To solve this, we will follow these steps −

  • pos_index := 0

  • Define one set stk another set aux

  • Define constructor, this is not doing any special task

  • Define a function push(), this will take val,

  • insert pos_index , val into stk

  • insert val, pos_index into aux

  • (increase pos_index by 1)

  • Define a function top()

  • if stk is empty, then −

    • return −1

  • return second value of first item of stk

  • Define a function max()

  • if aux is empty, then −

    • return −1

  • return first value of first item of aux

  • Define a function pop()

  • if stk is empty, then −

    • return −1

  • id := first value of first item of stk, ret = second value of first item of stk

  • delete first element of stk from stk

  • delete pair (ret, id) from aux

  • return ret

  • Define a function popmax()

  • if aux is empty, then −

    • return −1

  • ret := first value of first item of aux, id = second value of first item of aux

  • delete first element of aux from aux

  • delete pair(id, ret) from stk

  • return ret

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class MaxStk {
   int pos_index = 0;
   set<pair<int, int>, greater<>> stk, aux;
   public:
   MaxStk() {}
   void push(int val) {
      stk.emplace(pos_index, val);
      aux.emplace(val, pos_index);
      pos_index++;
   }
   int top() {
      if (stk.empty())
      return −1;
      return stk.begin()−>second;
   }
   int max() {
      if (aux.empty())
      return −1;
      return aux.begin()−>first;
   }
   int pop() {
      if (stk.empty())
      return −1;
      int id = stk.begin()−>first, ret = stk.begin()−>second;
      stk.erase(stk.begin());
      aux.erase({ret, id});
      return ret;
   }
   int popmax() {
      if (aux.empty())
      return −1;
      int ret = aux.begin()−>first, id = aux.begin()−>second;
      aux.erase(aux.begin());
      stk.erase({id, ret});
      return ret;
   }
};
int main(){
   MaxStk max_stk;
   max_stk.push(5);
   max_stk.push(15);
   max_stk.push(10);
   cout << max_stk.top() << endl;
   cout << max_stk.max() << endl;
   cout << max_stk.popmax() << endl;
   cout << max_stk.max() << endl;
   cout << max_stk.pop() << endl;
   cout << max_stk.top() << endl;
}

Input

max_stk.push(5)
max_stk.push(15)
max_stk.push(10)
max_stk.top()
max_stk.max()
max_stk.popmax()
max_stk.max()
max_stk.pop()
max_stk.top()

Output

10
15
15
10
10
5
raja
Published on 26-Dec-2020 11:10:03
Advertisements