Two queues to implement a stack two stacks implement a queue +

Interview handwriting often appear to let you two queues to implement a stack, stack two implementation issues a queue, it is a headache! Today I will analyze it carefully, thinking through the Java code to share with you :( a) two queues to implement a stack:

 

Two queues additive elements, which queue is empty, since the output element to move the respective elements (excluding trailing elements), so to add the corresponding elements is not empty queue; when the output data is to be disguised operation of two queues, the queue to be empty sequentially add elements to the empty queue is not until the last element output can be!

/ ** 
 * two queues to implement a stack 
 * @auther Yangchao 
 * @date 2019/7/18 
 * / 

public class TwoQueueImplStack { 

    Private Queue <Integer> = Queue1 new new ArrayDeque <> (); 

    Private Queue <Integer> = new new Queue2 ArrayDeque <> (); 

    / ** 
     * pushed into the data stack 
     * @param Element 
     * / 
    public void Push (Integer Element) { 
        // two queue is empty, a priority Queue1 
        IF (queue1.isEmpty () && queue2.isEmpty ()) { 
            queue1.add (Element); 
            return; 
        } 

        // null if queue1, Queue2 data, directly into Queue2 
        IF (queue1.isEmpty ()) { 
            queue2.add (Element); 
            return; 
        }

        // If queue1 is empty, Queue2 data, directly into Queue2 
        IF (queue2.isEmpty ()) { 
            queue1.add (Element); 
            return; 
        } 
    } 

    / ** 
     Data taken stack * 
     * @return 
     * / 
    public Integer poll () { 
        // when two queue is empty, is directly thrown 
        IF (queue1.isEmpty () && queue2.isEmpty ()) { 
            the throw a RuntimeException new new ( "empty Stack iS"); 
        } 

        // if queue1 is empty, queue2 successively added to the elements Queue1, the last element of the pop-up 
        IF (queue1.isEmpty ()) { 
            the while (queue2.size ()>. 1) { 
                queue1.add (queue2.poll ()); 
        } 
            }
            queue2.poll return (); 

        // if queue2 is empty, the elements queue1 successively added to queue2, the last element of the pop-up 
        IF (queue2.isEmpty ()) { 
            the while (queue1.size ()>. 1) { 
                queue2. the Add (queue1.poll ()); 
            } 
            return queue1.poll (); 
        } 
        return null; 
    } 

    public static void main (String [] args) { 
        TwoQueueImplStack TwoQueueImplStack new new QS = (); 
        qs.push (2); 
        QS. Push (. 4); 
        qs.push (. 7); 
        qs.push (. 5); 
        System.out.println (qs.poll ()); 
        System.out.println (qs.poll ()); 

        qs.push (. 1 ); 
        System.out.println (qs.poll ()) ; 
    } 

}

Output:

(Ii) two stacks implement a queue:

 

The first stack is only responsible for the additive element, a second pop-up element when the stack is first determined whether the current stack is empty, if the data is empty directly to its first pushed onto the stack a second stack, and then output of the top element, the effect can be achieved queue; if a second data stack, added directly to the data pushed onto the first stack, a second output directly to the elements of the output stack!

/ ** 
 * two stacks implement a queue 
 * @auther Yangchao 
 * @date 2019/7/18 
 * / 

public class TwoStackImplQueue { 

    Private Stack <Integer> = Stack1 new new Stack <> (); 

    Private Stack <Integer> = new new stack2 divided by 2. Stack <> (); 

    / ** 
     * Stack1 pressed into the queue element is only responsible 
     * @param element 
     * / 
    public void Push (Integer element) { 
        stack1.add (element); 
    } 

    / ** 
     * remove top of the queue elements 
     * @return 
     * / 
    public poll Integer () { 
        // If stack2 is empty, the element is pressed into the stack1 stack2 
        IF (stack2.isEmpty ()) { 
            the while (stack1.size ()> 0) {
                stack2.add(stack1.pop());
            }
        }
        if (stack2.isEmpty()) {
            throw new RuntimeException("queue is Empty!");
        }
        Integer head = stack2.pop();
        return head;
    }

    public static void main(String[] args) {
        TwoStackImplQueue sq = new TwoStackImplQueue();
        sq.push(1);
        sq.push(3);
        sq.push(5);
        sq.push(4);
        sq.push(2);

        System.out.println(sq.poll());
        System.out.println(sq.poll());

        sq.push(7);
        System.out.println(sq.poll());
    }

}

Output:

  A little bit of progress every day, continue to fuel ......

 

Guess you like

Origin www.cnblogs.com/blogtech/p/11208058.html