深入浅出数据结构——栈

栈(Stack)

概念

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出 LIFO(Last In First Out)的原则。

压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶

出栈:栈的删除操作叫做出栈。出数据在栈顶

在这里插入图片描述

栈的方法

方法 说明
E push(E item) 将元素放在栈顶
E pop() 将栈顶元素弹出
E peek() 返回栈顶元素
boolean empty() 判断栈是否为空
int search(Object o) 查找栈所处位置

测试方法:

        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println(stack.peek());   //3
        System.out.println(stack.pop());    //3
        System.out.println(stack.peek());   //2
        System.out.println(stack.empty());  //false
        System.out.println(stack.search(2));    //1
        System.out.println(stack.search(1));    //2

逆波兰式

中缀表达式转后缀表达式

在这里插入图片描述
计算机时不会处理中缀表达式的,一般都是把中缀转换成后缀,在进行如下运算的:
在这里插入图片描述

数组实现栈

public class MyStack {
    
    
    private int[] elem; //存放栈中元素
    private int top;    //代表下标,也代表当前有多少元素

    //默认开辟大小为10的栈
    public MyStack() {
    
    
        this.elem = new int[10];
    }

    //判断栈是否为满
    public boolean isFull() {
    
    
        return this.top == this.elem.length;
    }

    //给栈中压入元素
    public int push(int item) {
    
    
        if(isFull()) {
    
    
            throw new RuntimeException("栈为满");
        }
        this.elem[this.top] = item;
        this.top++;
        return this.elem[this.top-1];
    }

    //弹出栈顶元素
    public int pop() {
    
    
        if(empty()) {
    
    
            throw new RuntimeException("栈为空");
        }
        this.top--;
        return this.elem[this.top];
    }

    //显示栈顶元素
    public int peek() {
    
    
        if(empty()) {
    
    
            throw new RuntimeException("栈为空");
        }
        return this.elem[this.top-1];
    }

    //判断栈是否为空
    public boolean empty() {
    
    
        return this.top == 0;
    }

    //栈中元素个数
    public int size() {
    
    
        return this.top;
    }

}

用队列实现栈

力扣链接

class MyStack {
    
    
    Queue<Integer> q1;
    Queue<Integer> q2;

    /** Initialize your data structure here. */
    public MyStack() {
    
    
        q1 = new LinkedList<>();
        q2 = new LinkedList<>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
    
    
        //谁不为空入到哪个队列中
        if(q1.isEmpty() && q2.isEmpty()){
    
    
            q1.offer(x);
            return;
        }
        if(!q1.isEmpty()) {
    
    
            q1.offer(x);
        }
        if(!q2.isEmpty()) {
    
    
            q2.offer(x);
        }
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
    
    
        //每次出不为空的队列,出size-1个,到另一个为空的队列,最后弹出剩余的那个元素
        if(q1.isEmpty() && q2.isEmpty()) return -1;
        if(!q1.isEmpty()){
    
    
            int i = 1;
            int len = q1.size();
            while(i < len) {
    
    
                int en = q1.poll();
                q2.offer(en);
                i++;
            }
            return q1.poll();
        }
        if(!q2.isEmpty()){
    
    
            int i = 1;
            int len = q2.size();
            while(i < len) {
    
    
                int en = q2.poll();
                q1.offer(en);
                i++;
            }
            return q2.poll();
        }
        return -1;
    }

    /** Get the top element. */
    public int top() {
    
    
        if(q1.isEmpty() && q2.isEmpty()) return -1;
        if(!q1.isEmpty()){
    
    
            int i = 0;
            int en = 0;
            int len = q1.size();
            while(i < len) {
    
    
                en = q1.poll();
                q2.offer(en);
                i++;
            }
            return en;
        }
        if(!q2.isEmpty()){
    
    
            int i = 0;
            int en = 0;
            int len = q2.size();
            while(i < len) {
    
    
                en = q2.poll();
                q1.offer(en);
                i++;
            }
            return en;
        }
        return -1;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
    
    
        return q1.isEmpty() && q2.isEmpty();
    }

    public static void main(String[] args) {
    
    
        MyStack a = new MyStack();
        a.push(1);
        a.push(2);
        a.top();
    }
}

有效的括号

力扣链接

class Solution {
    
    
    public boolean isValid(String s) {
    
    
        Stack ret = new Stack();
        int i = 1;
        ret.push(s.charAt(0));
        while(i != s.length()) {
    
    
            if(ret.empty() || s.charAt(i) != change((char)ret.peek())) {
    
    
                ret.push(s.charAt(i));
            }else {
    
    
                ret.pop();
            }
            i++;
        }
        if(ret.empty()) return true;
        return false;
    }

    public char change(char a) {
    
    
        if(a == '(') return ')';
        if(a == '[') return ']';
        if(a == '{') return '}';
        return '?';
    }
}

最小栈

力扣链接

class MinStack {
    
    
    Stack<Integer> a;
    Stack<Integer> b;

    /** initialize your data structure here. */
    public MinStack() {
    
    
        a = new Stack<>();
        b = new Stack<>();
    }
    
    public void push(int x) {
    
    
        if(a.empty() || x <= b.peek()) {
    
    
            a.push(x);
            b.push(x);
        }else {
    
    
            a.push(x);
        }
    }
    
    public void pop() {
    
    
        if(a.pop().equals(b.peek())) {
    
    
            b.pop();
        }
    }
    
    public int top() {
    
    
        return a.peek();
    }
    
    public int getMin() {
    
    
        return b.peek();
    }
}

猜你喜欢

转载自blog.csdn.net/starry1441/article/details/114597582