栈(stack)

栈的介绍

  1. 栈是一个先入后出的有序列表
  2. 栈是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。允许插入和删除的一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)。
  3. 最先放入栈中的元素在栈底,最后放入的元素在栈顶。最后放入的元素先删除,最先放入的元素后删除。

数组模拟栈

  1. 思路分析

    1. 定义一个top来表示栈顶,初始化为-1
    2. 入栈操作,当有数据加入时,top++;stack[top]=data;
    3. 出栈操作,int value=stack[top];top–;return value
  2. 代码实现

    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;
    }

    //入栈
    public void push(int valule) {
    if (isFull()) {
    System.out.println("栈满");
    return;
    }
    top++;
    stack[top] = valule;
    }

    //出栈
    public int pop() {
    if (isEmpty()) {
    throw new RuntimeException("栈空");
    }
    int value =top;
    top--;
    return value;
    }


    //遍历栈,从栈顶开始显示数据
    public void list(){
    if(isEmpty()){
    System.out.println("栈空");
    return;
    }
    for (int i =top; i>=0 ; i--) {
    System.out.printf("stack[%d]=%d\n",i,stack[i]);
    }
    }
    }

栈实现综合计算器

  1. 思路分析

    1. 准备两个栈,一个数栈:用来存放数据 一个符号栈:用来存放运算符
    2. 通过一个index值(索引),来遍历我们的表达式
    3. 如果我们发现是一个数字,就直接入数栈
    4. 如果是符号
      1. 如果当前的符号栈是空,就直接入栈
      2. 如果符号栈不空,就进行比较,如果当前的操作符的优先级小于或等于栈中的操作符,就需要从数栈中pop出两个数,在从符号栈中pop出一个符号,进行运算,将得到的结果,入数栈,然后将当前的符号入符号栈,如果当前的符号的优先级大于栈中的操作符,就直接入符号栈
    5. 当表达式扫描完毕,就顺序的从数栈和符号栈中pop出相应的数和符号,并进行运算
    6. 最后在数栈的只有一个数字,就是表达式的结果
  2. 代码实现

    public class Calculator {
    public static void main(String[] args) {
    String expression ="7+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 =' ';
    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);
    }
    //最后
    index++;
    if (index>=expression.length()){
    break;
    }
    }
    //
    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("结果是:"+numStack.pop());
    }
    }

    //先创建一个栈
    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 int peek(){
    return stack[top];
    }


    //沾满
    public Boolean isFull() {
    return top == maxSize - 1;
    }


    //栈空

    public Boolean isEmpty() {
    return top == -1;
    }

    //入栈
    public void push(int valule) {
    if (isFull()) {
    System.out.println("栈满");
    return;
    }
    top++;
    stack[top] = valule;
    }

    //出栈
    public int pop() {
    if (isEmpty()) {
    throw new RuntimeException("栈空");
    }
    int value = stack[top];
    top--;
    return value;
    }


    //遍历栈,从栈顶开始显示数据
    public void list() {
    if (isEmpty()) {
    System.out.println("栈空");
    return;
    }
    for (int i = top; i >= 0; i--) {
    System.out.printf("stack[%d]=%d\n", 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;
    }
    }

栈的三种表达式

前缀表达式
  1. 前缀表达式又称波兰式,前缀表达式的运算符位于操作数之前
  2. 如:(3+4)*5-6对应的前缀表达式就是- * + 3 4 5 6 从右向左开始扫描
中缀表达式
  1. 中缀表达式 就是我们常见的运算表达式
  2. 对于计算机来说,中缀表达式很难表示
后缀表达式(逆波兰表达式)
  1. 与前缀表达式相似,只是运算符位于操作数之后

  2. 如:(3+4)5 - 6 对应的后缀表达式就是3 4 + 5 6 -

  3. 代码实现

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Stack;

    public class PolandNotation {
    public static void main(String[] args) {
    //定义一个逆波兰表达式
    //(3+4)X 5-6 =>3 4 + 5 X 6 -
    String suffixExpression = "3 4 + 5 * 6 -";
    //1.先将"3 4 + 5 X 6 -" 放到ArrayList中
    //2.将ArrayList传递给一个方法,遍历ArrayList配合栈完成计算

    List<String> rpnList = getListString(suffixExpression);
    System.out.println(rpnList);

    int res =calculate( rpnList);
    System.out.println(" "+res);
    }

    //将一个逆波兰表达式,依次将数据和运算符 放入到ArrayList中
    public static List<String> getListString(String suffixExpression) {
    //将suffixExpression 分割
    String[] split = suffixExpression.split(" ");
    List list = new ArrayList();
    for (String ele : split
    ) {
    list.add(ele);
    }
    return list;
    }

    //计算
    public static int calculate(List<String> ls) {
    //创建栈,
    Stack<String> stack = new Stack<String>();
    //遍历ls
    for (String item : ls) {
    //利用正则表达式来取出数
    if (item.matches("\\d+")) {
    stack.push(item);
    } else {
    //pop出两个数,并运算,再入栈
    int num2 = Integer.parseInt(stack.pop());
    int num1 = Integer.parseInt(stack.pop());
    int res = 0;
    if (item.equals("+")) {
    res = num1 + num2;
    } else if (item.equals("-")) {
    res = num1 - num2;
    } else if (item.equals("*")) {
    res = num1 * num2;
    } else if (item.equals("/")) {
    res = num1 / num2;
    } else {
    throw new RuntimeException("运算有误");
    }
    stack.push(res + " ");
    }
    }
    return Integer.parseInt(stack.pop());
    }
    }

中缀表达式转后缀表达式