Advertisement
nikeza

stack_Maximum Element

Sep 9th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3.  
  4. public class stack_Maximum_Element {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int num = scanner.nextInt();
  9.  
  10.         ArrayDeque<Integer> stack = new ArrayDeque<>();
  11.  
  12.         for (int i = 0; i < num; i++) {
  13.             int command = scanner.nextInt();
  14.             if (command == 1) {
  15.                 stack.push(scanner.nextInt());
  16.             } else if (command == 2) {
  17.                 if (!stack.isEmpty()){
  18.                     stack.pop();
  19.                 }
  20.  
  21.             } else if (command == 3) {
  22.                 int max = Integer.MIN_VALUE;
  23.                 int temp = 0;
  24.                 int count = stack.size();
  25.                 if (!stack.isEmpty()){
  26.  
  27.                     while (count > 0) {
  28.  
  29.                         temp = stack.pop();
  30.  
  31.                         stack.addLast(temp);
  32.  
  33.                         count--;
  34.                         if (temp > max) {
  35.                             max = temp;
  36.                         }
  37.                     }
  38.                     System.out.println(max);
  39.                 }
  40.             }
  41.         }
  42.  
  43.  
  44.  
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement