栈 -- 以及用栈实现计算器

7. 栈

情景带入:

​ 给定一串字符串"7*2*2-5+1-5+3-3",求出字符串的结果

7.1 栈的介绍

  • 栈的英文是(stack)
  • 栈是一个先进后出(FILO - First In Last Out)的有序列表
  • 栈(stack)是限制线性表中元素的插入和删除,只能在线性表的同一端进行的特殊线性表,允许插入和删除的一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)
  • 根据定义可知,最先放入栈中元素在栈底,最后放入栈中元素在栈顶,而删除元素刚好相反,最后放入的元素最先删除,最先放入的元素最后删除。
  • 出栈(pop)和入栈(push)

7.2 栈的应用场景

  1. 子程序的调用:在跳往子程序前,会先将下一个指令的地址存到堆栈中,直到子程序执行完后再将地址取出,以回到原来的程序中
  2. 处理递归调用:和子程序的调用类似,只是除了储存下一个指令的地址外,也将参数,区域变量等数据存入堆栈中
  3. 表达式的转换与求值
  4. 二叉树的遍历
  5. 图形的深度优先(depth - first)搜索法

7.3 实现栈的思路

  1. 使用数组来模拟
  2. 定义一个top,来表示栈顶,初始值为-1
  3. 入栈的操作,当有数据加入到栈时,top++;stack[top] = data;
  4. 出栈的操作,int value = stack[top]; top--; return value;
7.3.1 数组实现栈
// 定义一个ArrayStack 表示栈
class ArrayStack{
    private int maxSize; // 栈的大小
    private int[] stack; // 数组,数组模拟栈,数据就放在该数组
    private int top = -1; // top 表示栈顶,初始化为-1

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    // 判断栈满
    public boolean isFull(){
        return top == maxSize - 1;
    }

    // 栈空
    public boolean isEmpty(){
        return top == -1;
    }

    // 入栈-push
    public void push(int value){
        // 先判断是否满
        if (isFull()){
            System.out.println("Full");
            return;
        }
        top++;
        stack[top] = value;
    }
    // 出栈 pop 将栈顶的数据取出
    public int pop(){
        if (isEmpty()){
            System.out.println("Empty");
            // 抛出异常处理
            throw new RuntimeException("栈空,没有数据");
        }
        int value = stack[top];
        top--;
        return value;
    }

    // 显示栈, 遍历栈
    public void list(){
        // 遍历时需要从栈顶显示
        if (isEmpty()){
            System.out.println("Empty");
            return;
        }
        for (int i = top; i >= 0; i--){
            System.out.println("stack["+i+"]="+stack[i]);
        }
    }

}

测试上面的栈是否可用

public class ArrayStackDemo {
    public static void main(String[] args) {
        // 测试ArrayStack是否正确
        ArrayStack arrayStack = new ArrayStack(4);
        String key = "";
        boolean loop = true; //  是否退出菜单
        Scanner scanner = new Scanner(System.in);

        while (loop){
            System.out.println("show:表示显示栈");
            System.out.println("exit:退出栈");
            System.out.println("push:表示添加数据栈");
            System.out.println("pop:从栈中取出数据");

            System.out.println("请输入:");
            key = scanner.next();
            switch (key){
                case "show":
                    arrayStack.list();
                    break;
                case "push":
                    System.out.println("请输入一个数:");
                    int value = scanner.nextInt();
                    arrayStack.push(value);
                    break;
                case "pop":
                    try {
                        int res = arrayStack.pop();
                        System.out.println("出栈的数据是"+res);
                    } catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
            }
        }
        System.out.println("程序退出了");
    }
}
7.3.2 链表实现栈
package stack;

public class LinkeStackDemo {
    public static void main(String[] args) {
        LinkeStack ls = new LinkeStack();
        Node n1 =new Node(1, 10);
        Node n3 =new Node(3, 20);
        Node n2 =new Node(2, 30);
        Node n4 =new Node(4, 40);

        ls.list();

        ls.push(n1);
        ls.push(n2);
        ls.push(n3);
        ls.push(n4);
        ls.list();

        Node temp = ls.pop();
        System.out.println(temp.getId()+"编号的数值是:" + temp.getVal());
        temp = ls.pop();
        System.out.println(temp.getId()+"编号的数值是:" + temp.getVal());
        temp = ls.pop();
        System.out.println(temp.getId()+"编号的数值是:" + temp.getVal());

    }
}
class LinkeStack{
    private Node head = new Node(0,0);

    // 判断是否为空
    public boolean isEmpty(){
        return head.getNext() == null;
    }
    // push 添加数据
    public void push(Node node){
        Node temp = head;
        node.setNext(temp.getNext());
        temp.setNext(node);
    }

    // 取出数据
    public Node pop(){
        if (isEmpty()){
            System.out.println("Empty");
            throw new RuntimeException("Empty");
        }
        head = head.getNext();
        return head;
    }
    // 遍历表
    public void list(){
        if (isEmpty()){
            System.out.println("Empty");
            return;
        }
        Node temp = head.getNext();
        while (true){
            if (temp == null){
                break;
            }
            System.out.println(temp.getId()+"编号的数值是:" + temp.getVal());
            temp = temp.getNext();
        }
    }
}

class Node{
    private int id;
    private int val;
    private Node next;

    public Node(int id, int val) {
        this.id = id;
        this.val = val;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public Node(){
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

7.4 使用栈完成计算器

参考视频

使用栈完成表达式的计算思路

  1. 通过一个index值(索引) ,来遍历我们的表达式
  2. 如果我们发现是个数组,就直接加入数栈
  3. 如果发现,我们扫描到是一个符号,就分如下情况
    1. 如果发现当前符号栈为空,直接入栈
    2. 如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或者等于栈中的操作符,就需要从数栈中pop出两个数,再从符号栈中pop出一个符号,进行运算,得到结果,入数栈,然后将当前扫描的操作符入符号栈
    3. 如果当前的操作符的优先级大于栈中的操作符,直接如符号栈
  4. 当表达式扫描完毕,就顺序的从数栈和符号栈中pop出相应的数和符号,并运行
  5. 最后在数栈中的数字就是表达式
package stack;

public class Calculator {
    public static void main(String[] args) {
        // 根据思路,完成表达式的实现
        String expression = "30+2*6-2";
        // 创建两个栈,数栈,和符号栈
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);
        // 定义相关的变量
        int index = 0;// 用于扫描
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' '; // 将每次扫描得到的char保存到ch
        String keepNum = ""; // 用于拼接多为数
        // 开始while循环的扫描expression
        while (true){
            // 依次得到expression的每一个字符
            ch = expression.substring(index,index + 1).charAt(0);
            // 判断ch是什么,然后做相应的处理
            if (operStack.isOper(ch)){
                // 判断当前符号栈是否为空
                if (!operStack.isEmpty()){
                    // 处理
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())){
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        // 把运算的结果入数栈
                        numStack.push(res);
                        // 然后把当前的操作符入栈
                        operStack.push(ch);
                    }else {
                        // 如果优先级大于栈顶优先级,直接入栈
                        operStack.push(ch);
                    }
                } else {
                        // 如果为空直接入符号栈
                        operStack.push(ch);
                }
            } else {
                // numStack.push(ch - 48);
                // 当处理多位数时,不能发现是一个数就立即入栈,因为他可能是多位数
                // 在处理数,需要向 expression 的表达式的index 后再看以为,如果是数就进行扫描,如果是符号才入栈
                // 因此我们需要定义一个变量,用于拼接
                keepNum += ch;
                // 如果ch是最后一位,直接入栈
                if (index == expression.length() - 1){
                    numStack.push(Integer.parseInt(keepNum));
                } else {
                    // 判断下一位是不是数组,如果是,继续扫描,如果不是,则入栈
                    if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        // 如果后一位是运算符,则入栈
                        numStack.push(Integer.parseInt(keepNum));
                        // 重要!!!!清空keepNum;
                        keepNum = "";
                    }
                }

            }
            // 让index + 1, 并且判断是否扫描到expression最后
            index++;
            if (index >= expression.length()){
                break;
            }
        }

        // 当扫描完毕后,就顺序的从 数栈 和 符号栈 中pop出相应的数和符号进行运算
        while (true){
            // 如果符号栈为空,则计算到最后的结果,数栈中只有一个结果
            if (operStack.isEmpty()){
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            numStack.push(res); // 入栈
        }
        System.out.println("表达式是:"+ expression + " 的结果是"+ numStack.pop());

    }
}
// 先创建一个栈
// 定义一个ArrayStack 表示栈
class ArrayStack2{
    private int maxSize; // 栈的大小
    private int[] stack; // 数组,数组模拟栈,数据就放在该数组
    private int top = -1; // top 表示栈顶,初始化为-1

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    // 判断栈满
    public boolean isFull(){
        return top == maxSize - 1;
    }

    // 栈空
    public boolean isEmpty(){
        return top == -1;
    }

    // 入栈-push
    public void push(int value){
        // 先判断是否满
        if (isFull()){
            System.out.println("Full");
            return;
        }
        top++;
        stack[top] = value;
    }
    // 出栈 pop 将栈顶的数据取出
    public int pop(){
        if (isEmpty()){
            System.out.println("Empty");
            // 抛出异常处理
            throw new RuntimeException("栈空,没有数据");
        }
        int value = stack[top];
        top--;
        return value;
    }

    // 显示栈, 遍历栈
    public void list(){
        // 遍历时需要从栈顶显示
        if (isEmpty()){
            System.out.println("Empty");
            return;
        }
        for (int i = top; i >= 0; i--){
            System.out.println("stack["+i+"]="+stack[i]);
        }
    }
    // 返回运算符的优先级,优先级由程序员决定,优先级使用数字表示
    // 数字越大,优先级越高
    public int priority(int oper){
        if (oper =='*' || oper == '/'){
            return 1;
        } else if (oper == '+' || oper == '-'){
            return 0;
        } else {
            return -1;// 假定目前表达式只有 + - * / 四个符号
        }
    }
    // 判断是不是个运算符
    public boolean isOper(char val){
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    // 计算方法
    public int cal(int num1, int num2, int oper){
        int res = 0;
        switch (oper){
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1; // 注意顺序
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1; //  注意顺序
                break;
        }
        return res;
    }

    // 增加一个方法,可以返回当前栈顶的值,但不是pop
    public int peek(){
        return stack[top];
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值