Implementation of Maximum Queue
Example 1:
Input: push={5,3,40} ,getmaximum ,push={60,50}, getmaximum, pop, getmaximum
Output: 40, 60, 60
Approach:
Java
import java.util.Stack;import javafx.util.Pair;@SuppressWarnings("restriction")public class MaximumQueue {public static void main(String[] args) {// stack pair to hold the element and the// maximum at the top of stackStack<Pair<Integer, Integer>> stack1 =new Stack<Pair<Integer, Integer>>();Stack<Pair<Integer, Integer>> stack2 =new Stack<Pair<Integer, Integer>>();// add element'saddELement(stack1, 5);addELement(stack1, 3);addELement(stack1, 10);// find maximum elementSystem.out.print(findmaximumElement(stack1, stack2) + " ");// add again element'saddELement(stack1, 12);addELement(stack1, 4);// find maximumSystem.out.print(findmaximumElement(stack1, stack2) + " ");removeElement(stack1, stack2);// find maximumSystem.out.print(findmaximumElement(stack1, stack2) + " ");}private static void removeElement(Stack<Pair<Integer, Integer>>stack1, Stack<Pair<Integer, Integer>> stack2) {// if second is emptyif (stack2.isEmpty()) {// this means convert the stack into queuewhile (!stack1.isEmpty()) {int element = stack1.pop().getValue();int maximum;// if second is empty// maximum is the current elementif (stack2.isEmpty())maximum = element;// else the maximum is max of current// element and stack topelsemaximum = Math.max(element, stack2.peek().getValue());// push into the new stackstack2.add(new Pair<Integer, Integer>(element, maximum));}}// if second stack is not empty then pop the top elementif (!stack2.isEmpty())stack2.pop();}private static int findmaximumElement(Stack<Pair<Integer, Integer>>stack1, Stack<Pair<Integer, Integer>> stack2) {// if both stack is empty then -1if (stack1.isEmpty() && stack2.isEmpty())return -1;// if stack1 is empty then// top of stack2else if (stack1.isEmpty()) {return stack2.peek().getValue();// if stack2 is empty then// top of stack1} else if (stack2.isEmpty()) {return stack1.peek().getValue();// find max of both stack1, stack2} else {return Math.max(stack2.peek().getValue(),stack1.peek().getValue());}}private static void addELement(Stack<Pair<Integer, Integer>>stack1, int value) {int max = 0;// if stack is empty then// the maximum element is the given elememtif (stack1.isEmpty())max = value;// else the maximum element is// the max of stack top and current elementelse {max = Math.max(stack1.peek().getValue(), value);}// push the current element// with the maximum elementstack1.add(new Pair<Integer, Integer>(value, max));}}
C++
#include <bits/stdc++.h>using namespace std;//function to add the new elementvoid addElement(stack<pair<int,int>> &st1,int value){int maximum;//if stack is empty//then maximum element is the//new elementif(st1.empty())maximum=value;//else the maximum element//is the max of stack top and current//elementelsemaximum=max(value,st1.top().second);//push the new element into the stackst1.push({value,maximum});}//function to find the maximum element//of the queueint findMaximumElement(stack<pair<int,int>> &st1,stack<pair<int,int>> &st2){//if both are empty return -1if(st1.empty()&&st2.empty())return -1;//if first is empty return//second top elementelse if(st1.empty())return st2.top().second;//if second is empty return//first top elementelse if(st2.empty())return st1.top().second;//else return max of both//stackselsereturn max(st1.top().second,st2.top().second);}//function to remove the elementvoid removeElement(stack<pair<int,int>> &st1,stack<pair<int,int>> &st2){//if second is emptyif(st2.empty()){//this means convert the stack into//queuewhile(!st1.empty()){int element=st1.top().first;st1.pop();int maximum;//if second is empty//maximum is the current elementif(st2.empty())maximum=element;//else the maximum//is max of current element and//stack topelsemaximum=max(element,st2.top().second);//push into the new stackst2.push({element,maximum});}}//if second stack is not//empty then pop the top//elementif(!st2.empty())st2.pop();}int main(){stack<pair<int,int>> st1,st2;addElement(st1,5);addElement(st1,3);addElement(st1,40);//print 40cout<<findMaximumElement(st1,st2)<<"\n";addElement(st1,60);addElement(st1,50);//print 60cout<<findMaximumElement(st1,st2)<<"\n";removeElement(st1,st2);//print 60cout<<findMaximumElement(st1,st2)<<"\n";return 0;}
No comments:
Post a Comment