栈与队列

133 阅读2分钟

1.概念

栈:先进后出; 队列:先进先出

(1) 双向链表实现代码实现

(2) 数组实现代码实现

(3)实现一个特殊的栈,在基本功能的基础上,再实现返回栈中最小元素的功能

  • pop push getMin操作时间的时间复杂度为O(1)
  • 设计的栈类型可使用现成的栈结构

思路:数据栈与最小栈。第一个数同时进入数据栈与最小栈。如果进入的数 比最小栈 的栈顶小,压入当前最小栈的栈顶;如果大就重复压入当前最小栈的栈顶

image.png

    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;
    
    public MyStack(){
        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("stack is empty");
        }
        this.stackMin.pop();
    }
    public int getmin() {
        if (this.stackData.isEmpty()) {
            throw new RuntimeException("stack is empty")
        }
        return this.stackMin.peek();
    }
}

image.png

    public Stack<Integer> stackPush;//push栈
    public Stack<Integer> stackPop;//pop栈
    
    public TwoStackQueue() {
        stackPush = new Stack<Integer>();
        stackPop = new Stack<Integer>();
    }
    //push栈向pop栈倒入数据
    private void pushToPop() {
        if (stackPop.empty()) {                //push栈放入数据,当push栈不空时放入数据
            while (!stackPush.empty()) {//当push栈全部为空时---全部倒到pop栈中
                stackPop.push(stackPush.pop());//push栈数据弹出到pop栈 
            }
        }
    }
    
    public void add (int pushInt) {//添加数据直接进push栈
        stackPush.push(pushInt);//判断能否倒数据
        pushToPop();
    }
    
    public int poll() {//如果拿数据,
        if (stackPop.empty() && stackPush.empty()) {
            throw new RuntimeException("Queue is empty!");
        }
        pushToPop();//检查能否倒数据
        return stackPop.pop();//从pop中拿出一个
    }
    
    public int peek() {//如果拿数据,
        if (stackPop.empty() && stackPush.empty()) {
            throw new RuntimeException("Queue is empty!");
        }
        pushToPop();//检查能否倒数据
        return stackPop.peek();//从peek中拿出一个
    }
}

拓展:pop与peek的异同

相同点:* 都可以取出栈顶的值*

不同点:
peek 不会改变栈的值(不删除栈顶的值)

pop会把栈顶的值删除。

(5)用两个队列形成栈

思路:如果要实现一组数1,2,3,4,5.返回为5,4,3,2,1。

image.png

    
    public Queue<T> queue;
    public Queue<T> help;
    
    public TwoQueueStack() {
       queue = new LinkedList<>();
        help = new LinkedList<>();
    }
    
    public void push(T value) {
        queue.offer(value);//直接加数据
    }
    
    public T poll() {
        while (queue.size() > 1) {
            //queue只有一个数据的时候
            help.offer(queue.poll());
        }
        T ans = queue.poll();//将最后一个数当做ans,并返回
        Queue<T> tmp = queue;//互换help与queue
        queue = help;
        help = tmp;
        return ans;
    }
    
    public T peek() {
        while (queue.size() > 1) {
            help.offer(queue.poll());
        }
        T ans = queue.poll();
        help.offer(ans);
        Queue<T> tmp = queue;
        queue = help;
        help = tmp;
        return ans;
    }
    
    public boolean isEmpty() {
        return queue.isEmpty();
    }
}

//测试用例
public static void main(String[] args) {
    new 
}