
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to construct Maximum Stack with given operations in C++
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
#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
- Related Articles
- Program to construct Frequency Stack in C++
- C# Program to Implement Stack with Push and Pop operations
- Program to check final answer by performing given stack operations in Python
- Count of suffix increment/decrement operations to construct a given array in C++
- Maximum Possible Product in Array after performing given Operations in C++
- Maximum Frequency Stack in C++
- Construct a BST from given postorder traversal using Stack in Python
- Maximum count of equal numbers in an array after performing given operations in C++
- C++ program to construct graph with certain conditions
- Find maximum in stack in O(1) without using additional stack in C++
- Find maximum operations to reduce N to 1 in C++
- Data Structures Stack Primitive Operations
- Program to find maximum score from performing multiplication operations in Python
- C++ Program to Implement Stack
- C++ program to count number of cities we can visit from each city with given operations
