Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- public class MyQueue<Integer> {
- public Stack<Integer> s1;
- public Stack<Integer> s2;
- public Integer minEle;
- public MyQueue() {
- s1 = new Stack<>();
- s2 = new Stack<>();
- }
- public boolean isEmpty(){
- return s1.isEmpty() && s2.isEmpty();
- }
- public int size(){
- return s1.size() + s2.size();
- }
- public void enqueue(Integer e){
- if (s1.isEmpty()) {
- minEle = e;
- s1.push(e);
- } else if (e < minEle){
- }
- }
- public Integer dequeue(){
- if (s2.isEmpty()) {
- while (!s1.isEmpty()){
- s2.push(s1.pop());
- }
- }
- return s2.pop();
- }
- public static void main(String[] args) {
- MyQueue<java.lang.Integer> queue = new MyQueue<>();
- // Use the enqueue method to add three elements to the empty queue
- queue.enqueue(1);
- queue.enqueue(2);
- queue.enqueue(3);
- // The queue is no longer empty, it now contains three elements.
- System.out.println("There are " + queue.size() + " elements in the queue.");
- System.out.println();
- // We remove elements one by one using the dequeue method, until the queue is empty. Each time we remove an element
- // we print it out.
- while (!queue.isEmpty()) {
- System.out.println(queue.dequeue() + " ");
- }
- System.out.println();
- // The queue is now contains zero elements, as they have each been removed using dequeue().
- System.out.println("There are " + queue.size() + " elements in the queue.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement