Advertisement
exmkg

Untitled

Sep 28th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. class MyQueue {
  2.  
  3.     Stack<Integer> input = new Stack();
  4.     Stack<Integer> output = new Stack();
  5.    
  6.     public void push(int x) {
  7.         input.push(x);
  8.     }
  9.  
  10.     public int pop() {
  11.         int x = peek();
  12.         output.pop();
  13.         return x;
  14.     }
  15.  
  16.     public int peek() {
  17.         if (output.empty())
  18.             while (!input.empty())
  19.                 output.push(input.pop());
  20.         return output.peek();
  21.     }
  22.  
  23.     public boolean empty() {
  24.         return input.empty() && output.empty();
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement