/*
包含min函数的栈
*/
//思路:设计一个辅助栈来放最小数
import java.util.*;
public class Class25 {
Stack data = new Stack();
Stack auxiliary = new Stack();
public void push(int node){
data.push(node);
if(auxiliary.empty() || auxiliary.peek() > node){
auxiliary.push(node);
}else{
auxiliary.push(auxiliary.peek());
}
}
public void pop(){
if(data.pop() == auxiliary.peek()){
auxiliary.pop();
}
}
public int min(){
return auxiliary.peek();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
原文:https://www.cnblogs.com/zhuozige/p/12507346.html