数据结构:栈

这篇博客介绍了栈的数据结构及其用数组实现的详细过程,包括入栈、出栈等操作。此外,还展示了如何使用栈实现一个简易的计算器,能够处理加减乘除的运算,包括运算符的优先级处理。通过示例代码解释了计算器的运行逻辑,并给出了测试用例及结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、栈

栈(stack)又名堆栈,它是一种运算受限的线性表。

  1. 限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。
  2. 向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;
  3. 从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。

在这里插入图片描述

二、栈的实现(数组)

public class ArrayStack {


    /**
     * 栈的大小
     */
    private int maxSize;


    /**
     * 栈顶
     */
    private int top;


    /**
     * 实现栈的数组
     */
    private int[] array;


    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        this.top = -1;
        this.array = new int[maxSize];
    }


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


    /**
     * 判断栈是否为空
     * @return
     */
    public boolean isEmpty(){
        return top == -1;
    }


    /**
     * 入栈
     * @param n
     */
    public void push(int n){

        if (isFull()){
            System.out.println("栈满");
            return;
        }

        top++;
        array[top] = n;

    }


    /**
     * 出栈
     * @return
     */
    public int pop(){
        if (isEmpty()){
            System.out.println("栈空");
            return -1;
        }

        int value = array[top];

        top--;

        return value;

    }

    /**
     * 栈有效值
     * @return
     */
    public int size(){
        return top+1;
    }


    /**
     * 打印栈
     */
    public void print(){

        if (isEmpty()){
            System.out.println("栈空");
            return;
        }

        for (int i = 0;i <= top;i++){
            System.out.println(array[i]);
        }


    }


}

三、栈的使用实例(简易计算器)

1、项目结构

在这里插入图片描述

2、运算符实体类:Operator
/**
	运算符实体类(只涉及加减乘除)
 */
public class Operator {


    /**
     * 判断字符是否未运算符
     * @param c
     * @return
     */
    public static boolean isOperator(char c){
        return c == '+' || c == '-' || c == '*' || c == '/';
    }


    /**
     * 运算符优先级
     * @param c
     * @return
     */
    public static int operatorPriority(char c){
        if (c == '+' || c == '-'){
            return 0;
        }else if (c == '*' || c == '/'){
            return 1;
        }else{
            return -1;
        }
    }

    /**
     * 运算
     * @param c
     * @param x
     * @param y
     * @return
     */
    public static int operation(char c,int x,int y){

        switch (c){
            case '+' :
                return x+y;
            case '-' :
                return x-y;
            case '*' :
                return x*y;
            case '/' :
                return x/y;
        }

        return -1;
    }
}
3、简单的计算器(只支持个位数的加减乘除运行)
import java.util.*;

/**
	简单的计算器
 */
public class SimpleCalculator {

    /**
     * 数栈
     */
    ArrayStack number = new ArrayStack(99);


    /**
     * 运算符栈
     */
    ArrayStack operator = new ArrayStack(99);



    public int run(String s){

        int index = 0;

        char c = ' ';

        int result = 0;

        while (true){

            c = s.substring(index,index+1).charAt(0);

            if (c == ' '){
                index ++;
                continue;
            }

            if (Operator.isOperator(c)){

                if (operator.isEmpty()){

                    operator.push(c);

                }else{

                    int o1 = operator.pop();

                    int p1 = Operator.operatorPriority((char) o1);

                    int p2 = Operator.operatorPriority(c);

                    if (p2 <= p1){

                        int n1 = number.pop();
                        int n2 = number.pop();
                        result = Operator.operation((char) o1,n2,n1);
                        number.push(result);
                        operator.push(c);

                    }else{
                        operator.push(o1);
                        operator.push(c);
                    }
                }

            }else{

                number.push(Integer.parseInt(s.substring(index,index+1).toString()));

            }

            if (index == s.length()-1){

                boolean flag = !number.isEmpty();

                while (flag){

                    int n1 = number.pop();

                    int n2 = number.pop();

                    int c1 = operator.pop();

                    result = Operator.operation((char) c1,n2,n1);

                    if (number.isEmpty()){
                        break;
                    }

                    number.push(result);

                }

                break;

            }

            index++;


        }

        return result;

    }

}
4、测试
public class Test {

    public static void main(String[] args) {

        SimpleCalculator simpleCalculator = new SimpleCalculator();

        System.out.println(simpleCalculator.run("1+2+3+4"));
        System.out.println(simpleCalculator.run("1-2-3-4"));
        System.out.println(simpleCalculator.run("1*2*3*4"));
        System.out.println(simpleCalculator.run("1/2/3/4"));
        System.out.println(simpleCalculator.run("1+2-3*4/1"));

    }

}
5、结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pIeZO03R-1639631195806)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20211216130516961.png)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大树下躲雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值