Advertisement
Guest User

Start of Queue Enqueue/Dequeue/Min

a guest
Sep 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1.  
  2.  
  3. package com.company;
  4. import java.util.*;
  5.  
  6. public class MyQueue<Integer> {
  7.     public Stack<Integer> s1;
  8.     public Stack<Integer> s2;
  9.     public Integer minEle;
  10.  
  11.  
  12.     public MyQueue() {
  13.         s1 = new Stack<>();
  14.         s2 = new Stack<>();
  15.     }
  16.  
  17.  
  18.     public boolean isEmpty(){
  19.         return s1.isEmpty() && s2.isEmpty();
  20.     }
  21.  
  22.  
  23.     public int size(){
  24.         return s1.size() + s2.size();
  25.     }
  26.  
  27.  
  28.     public void enqueue(Integer e){
  29.         if (s1.isEmpty()) {
  30.             minEle = e;
  31.             s1.push(e);
  32.         } else if (e < minEle){
  33.  
  34.         }
  35.  
  36.  
  37.  
  38.  
  39.     }
  40.  
  41.  
  42.     public Integer dequeue(){
  43.         if (s2.isEmpty()) {
  44.             while (!s1.isEmpty()){
  45.                 s2.push(s1.pop());
  46.             }
  47.         }
  48.         return s2.pop();
  49.     }
  50.  
  51.  
  52.  
  53.  
  54.     public static void main(String[] args) {
  55.         MyQueue<java.lang.Integer> queue = new MyQueue<>();
  56.  
  57.         // Use the enqueue method to add three elements to the empty queue
  58.         queue.enqueue(1);
  59.         queue.enqueue(2);
  60.         queue.enqueue(3);
  61.  
  62.         // The queue is no longer empty, it now contains three elements.
  63.         System.out.println("There are " + queue.size() + " elements in the queue.");
  64.         System.out.println();
  65.  
  66.         // We remove elements one by one using the dequeue method, until the queue is empty. Each time we remove an element
  67.         // we print it out.
  68.         while (!queue.isEmpty()) {
  69.             System.out.println(queue.dequeue() + " ");
  70.         }
  71.  
  72.         System.out.println();
  73.         // The queue is now contains zero elements, as they have each been removed using dequeue().
  74.         System.out.println("There are " + queue.size() + " elements in the queue.");
  75.  
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement