Implement Stack using Two Queues

后端 未结 23 825
感情败类
感情败类 2020-11-28 17:05

A similar question was asked earlier there, but the question here is the reverse of it, using two queues as a stack. The question...

Given two queues with their sta

23条回答
  •  青春惊慌失措
    2020-11-28 17:33

    import java.util.*;
    
    /**
     *
     * @author Mahmood
     */
    public class StackImplUsingQueues {
    
        Queue q1 = new LinkedList();
        Queue q2 = new LinkedList();
    
        public int pop() {
            if (q1.peek() == null) {
                System.out.println("The stack is empty, nothing to return");
                int i = 0;
                return i;
            } else {
                int pop = q1.remove();
                return pop;
            }
        }
    
        public void push(int data) {
    
            if (q1.peek() == null) {
                q1.add(data);
            } else {
                for (int i = q1.size(); i > 0; i--) {
                    q2.add(q1.remove());
                }
                q1.add(data);
                for (int j = q2.size(); j > 0; j--) {
                    q1.add(q2.remove());
                }
    
            }
        }
    
        public static void main(String[] args) {
            StackImplUsingQueues s1 = new StackImplUsingQueues();
            //       Stack s1 = new Stack();
            s1.push(1);
            s1.push(2);
            s1.push(3);
            s1.push(4);
            s1.push(5);
            s1.push(6);
            s1.push(7);
            s1.push(8);
            s1.push(9);
            s1.push(10);
            // s1.push(6);
            System.out.println("1st = " + s1.pop());
            System.out.println("2nd = " + s1.pop());
            System.out.println("3rd = " + s1.pop());
            System.out.println("4th = " + s1.pop());
            System.out.println("5th = " + s1.pop());
            System.out.println("6th = " + s1.pop());
            System.out.println("7th = " + s1.pop());
            System.out.println("8th = " + s1.pop());
            System.out.println("9th = " + s1.pop());
            System.out.println("10th= " + s1.pop());
        }
    }
    

提交回复
热议问题