栈中返回最小值

/**
 * 实现一个特殊的栈,在实现栈的基本功能的基础上,在实现返回栈中最小元素的操作
 */
import java.util.Stack;

public class MyStack1 {
    private Stack<Integer> stackData;//数据栈,保存当前栈中的元素
    private Stack<Integer> stackMin;//最小值栈,保存每一步的最小值
    public MyStack1(){
        this.stackData = new Stack<Integer>();
        this.stackMin = new Stack<Integer>();
    }

    //向最小栈和数据栈输入元素
    public void push(int newNum){
        //如果最小值栈为空或存在值为小于输入元素,将输入元素放在其栈顶
        if(this.stackMin.isEmpty()){
            this.stackMin.push(newNum);
        }else if(newNum <= this.getMin()){
            this.stackMin.push(newNum);
        }
        this.stackData.push(newNum);//添加一个元素
    }

    //弹出数据规则
    public int pop(){
        if(this.stackData.isEmpty()){
            throw new RuntimeException("Your stack is empty.");
        }
        int value = this.stackData.pop();
        if(value == this.getMin()){
            this.stackMin.pop();//弹出栈顶元素,并删除栈顶元素
        }
        return value;
    }
    //得到最小值
    public int getMin(){
        if(this.stackMin.isEmpty()){
            throw new RuntimeException("Your stack is rmpty");
        }
        return  this.stackMin.peek();//返回栈顶元素
    }
    //输入一组数据
    public void input(int[] ints){
        for (int i = 0; i < ints.length; i++) {
            push(ints[i]);
        }
        System.out.println("MyStack1数据栈:"+stackData);
        System.out.println("Mystack1最小值栈:"+stackMin);
    }
}


import java.util.Stack;

public class MyStack2 {
    private Stack<Integer> stackData;//数据栈,存储所有数据
    private Stack<Integer> stackMin;//最小值栈,存储最小值序列
    public MyStack2(){
        this.stackData = new Stack<>();
        this.stackMin = new Stack<>();
    }
    //压入数据规则
    public void push(int newNum){
        if(this.stackMin.isEmpty()){
            this.stackMin.push(newNum);
        }else if(newNum < this.getMin()){
            this.stackMin.push(newNum);
        }else {
            int newMin = this.stackMin.peek();
            this.stackMin.push(newMin);
        }
        this.stackData.push(newNum);//添加一个元素
    }

    public int pop(){
        if(this.stackData.isEmpty()){
            throw new RuntimeException("Your stack is empty.");
        }
        this.stackMin.pop();//
        return this.stackData.pop();
    }

    public int getMin(){
        if(this.stackMin.isEmpty()){
            throw new RuntimeException("Your stack is empty.");
        }
        return this.stackMin.peek();
    }

    public void input(int[] ints){
        for (int i = 0; i < ints.length; i++) {
            push(ints[i]);
        }
        System.out.println("Mystack2数据栈:"+stackData);
        System.out.println("Mystack2最小值栈:"+stackMin);
    }
}


其中,stack中peek和pop的区别:

//pop和peek的区别
// 相同点:都返回栈顶的值
// 不同点:pop会把栈顶的值删掉
//        peek不会删除栈顶的值

public class Test {
    public static void main(String[] args){
        MyStack1 myStack1 = new MyStack1();
        MyStack2 myStack2 = new MyStack2();
        int[] ints = {9,2,5,6,3,1,4,6};
        myStack1.input(ints);
        /**
         * 多次执行pop方法,每执行一次,原数据栈栈顶元素删除一个,当数据栈顶元素与最小值栈顶元素相等时,将最小值栈顶元素也删除。同时数据栈中目前的最小值发生变化
         */
//        System.out.println(myStack1.pop());//数据栈不断弹出元素与最小栈顶元素比较,如果相等则弹出最小栈顶元素
//        System.out.println(myStack1.pop());//数据栈不断弹出元素与最小栈顶元素比较,如果相等则弹出最小栈顶元素
//        System.out.println(myStack1.pop());//数据栈不断弹出元素与最小栈顶元素比较,如果相等则弹出最小栈顶元素
        System.out.println("Mystack1最小值为:"+myStack1.getMin());

        myStack2.input(ints);
//        System.out.println(myStack1.pop());//数据栈和最小栈同时弹出栈顶元素
        System.out.println("Mystack2最小值为:"+myStack2.getMin());
    }
}



猜你喜欢

转载自blog.csdn.net/x_i_xw/article/details/78966357
今日推荐