package com.sunshine.OFFER66_SECOND; import org.junit.Test; import java.util.Stack; public class A5_pushpop { @Test public void test(){ push(1); push(2); push(3); push(4); System.out.println(pop()); System.out.println(pop()); System.out.println(pop()); System.out.println(pop()); } Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } public int pop() { if(!stack2.empty()){ return stack2.pop(); } while(!stack1.empty()){ stack2.push(stack1.pop()); } return stack2.pop(); } }
原文:https://www.cnblogs.com/MoonBeautiful/p/11417673.html