栈的一个实际需求
请输入一个表达式
计算式:[722-5+1-5+3-3] 点击计算
请问:计算机底层是如何运算得到结果的?
注意不是简单的把算式列出运算,因为我们看这个算式很简单
但是计算机怎么理解这个算式的(对计算机而言,它接收到的就是一个字符串),我们讨论的是这个问题。
栈的介绍
1)栈的英文为(stack)
2)栈是一个先入后出(FILO-First In Last Out)的有序列表。
3)栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。允许插入和删除的
一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)。
4)根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元
素最先删除,最先放入的元素最后删除
5)图解方式说明出栈(pop)和入栈(push)的概念尚硅谷Java数据结构和算法
栈的应用场景
- 子程序的调用:在跳往子程序前,会先将下个指令的地址存到堆栈中,直到子程序执行完后再将地址取出,以回到原来的程序中。
- 处理递归调用:和子程序的调用类似,只是除了储存下一个指令的地址外,也将参数、区域变量等数据存入堆栈中。
- 表达式的转换[中缀表达式转后缀表达式]与求值(实际解决)。
- 二叉树的遍历。
-
栈的快速入门
用数组模拟栈的使用,由于栈是一种有序列表,当然可以使用数组的结构来储存栈的数据内容,
下面我们就用数组模拟栈的出栈,入栈等操作。-
数组实现
/*** 数组实现* 0.存储数据* 1.构造器* 2.满* 3.空* 4.push* 5.pop* 6.遍历*/public class ArrayStack {private int[] array;private int size;private int top=-1;public ArrayStack(int size) {this.size = size;array = new int[size];}public int getSize() {return size;}public boolean isFull(){return top == size - 1;}public boolean isEmpty(){return top == -1;}public void push(int val) {if (isFull()) {throw new RuntimeException("栈已满,无法push");}top++;array[top] = val;}public int pop(){if (isEmpty()) {throw new RuntimeException("栈是空的,无法pop");}int result = array[top];top--;return result;}public void show(){for (int i = top; i >=0 ; i--) {System.out.printf("stack[%d]=%d\n",i,array[i]);}}public static void main(String[] args) {ArrayStack arrayStack = new ArrayStack(5);arrayStack.push(1);arrayStack.push(2);arrayStack.push(3);arrayStack.push(4);arrayStack.push(5);arrayStack.show();System.out.println(arrayStack.pop());System.out.println(arrayStack.pop());System.out.println(arrayStack.pop());}}
第一次用双链表
```java package com.sgg.datastructure.stack;
/**
- 链表实现
- 0.存储数据
- 1.构造器
- 2.满
- 3.空
- 4.push
- 5.pop
6.遍历 */ public class LinkStackTwo { private StackNodeTwo top;
public boolean isFull() {
return false;
}
public boolean isEmpty() {
return top == null;
}
public void push(int i) {
if (top == null) {top = new StackNodeTwo(i);top.next = top;top.prev = top;} else {StackNodeTwo node = new StackNodeTwo(i);top.next = node;node.prev = top;top = node;}
}
public int pop() {
if (isEmpty()) {throw new RuntimeException("空了");}int no = top.no;if (top != top.prev) {top = top.prev;} else {top = null;}return no;
}
public void show() {
StackNodeTwo temp = top;while (true) {System.out.printf("遍历%d\n",temp.no);temp = temp.prev;if (temp.prev == temp) {System.out.println("遍历"+temp.no);break;}}
}
public static void main(String[] args) {
LinkStackTwo linkStack = new LinkStackTwo();linkStack.push(1);linkStack.push(2);linkStack.push(3);linkStack.push(4);linkStack.show();System.out.println(linkStack.pop());System.out.println(linkStack.pop());System.out.println(linkStack.pop());System.out.println(linkStack.pop());
} }
<a name="Adk4u"></a>## 第二次看别人用单链表更合理```javapackage com.sgg.datastructure.stack;/*** 链表实现* 0.存储数据* 1.构造器* 2.满* 3.空* 4.push* 5.pop* 6.遍历*/public class LinkStackOne {private StackNodeOne top;public boolean isEmpty() {return top == null;}public void push(int i) {StackNodeOne node = new StackNodeOne(i);node.next = top;top = node;}public int pop() {if (isEmpty()) {throw new RuntimeException("空");}int no = top.no;top = top.next;return no;}public void show() {StackNodeOne temp = top;while (temp != null) {System.out.println("loop" + temp.no);temp = temp.next;}}public static void main(String[] args) {LinkStackOne linkStack = new LinkStackOne();linkStack.push(1);linkStack.push(2);linkStack.push(3);linkStack.push(4);linkStack.show();System.out.println(linkStack.pop());System.out.println(linkStack.pop());System.out.println(linkStack.pop());linkStack.push(5);linkStack.push(6);linkStack.push(7);linkStack.show();}}
利用栈,实现计算器
中缀直接计算
思路
1.遍历字符串,把数字和操作符分别放到2个栈
2.数字就是无脑入栈
3.操作符入栈有以下几种情况
- 操作符栈顶为空
- 当前操作符的优先级小于或者等于操作符栈顶的,取出栈顶,pop2个数,计算,入数栈,当前操作符入栈
- 优先级大于,入栈
代码实现
后缀表达式
思路
3 4 + 5 × 6 -的计算
1.从左至右扫描,将3和4压入堆栈;
2.遇到+运算符,因此弹出4和3(4为栈顶元素,3为次顶元素,注意与前缀表达式做比较),计算出3+4的值,得7,再将7入栈;
3.将5入栈;
4.接下来是×运算符,因此弹出5和7,计算出7×5=35,将35入栈;
5.将6入栈;
6.最后是-运算符,计算出35-6的值,即29,由此得出最终结果。
代码
public class 后缀表达式计算器 {public static String cal(String expression) {Stack<String> stack = new Stack<>();List<String> list = StrUtil.split(expression, StrUtil.SPACE);for (String str : list) {if (NumberUtil.isNumber(str)) {stack.push(str);}else {Integer pop1 = Integer.valueOf(stack.pop());Integer pop2 = Integer.valueOf(stack.pop());Integer result = calOne(pop1, pop2, str);stack.push(result+"");}}String result = stack.pop();System.out.printf("后缀表达式:%s = %s\n",expression,result);return result;}private static Integer calOne(Integer pop1, Integer pop2, String str) {Integer result = null;switch (str) {case "+":result = pop2 + pop1;break;case "-":result = pop2 - pop1;break;case "*":result = pop2 * pop1;break;case "/":result = pop2 / pop1;break;default:throw new RuntimeException("参数有误");}return result;}public static void main(String[] args) {String expression = "3 4 + 5 * 6 -";//29cal(expression);String expression2 = "4 5 * 8 - 60 + 8 2 / +";//76cal(expression2);}}
结果
后缀表达式:3 4 + 5 6 - = 29
后缀表达式:4 5 8 - 60 + 8 2 / + = 76
用后缀表达式计算结果还是很简单的
中缀表达式->后缀表达式
思路
- 初始化两个栈:运算符栈s1和储存中间结果的栈s2;
- 从左至右扫描中缀表达式;
- 遇到操作数时,将其压s2;
- 遇到运算符时,比较其与s1栈顶运算符的优先级:
- 如果s1为空,或栈顶运算符为左括号“(”,则直接将此运算符入栈;
- 否则,若优先级比栈顶运算符的高,也将运算符压入s1;
- 否则,将s1栈顶的运算符弹出并压入到s2中,再次转到(4-1)与s1中新的栈顶运算符相比较;
- 遇到括号时:
(1)如果是左括号“(”,则直接压入s1
(2)如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃 - 重复步骤2至5,直到表达式的最右边
- 将s1中剩余的运算符依次弹出并压入s2
- 依次弹出s2中的元素并输出,结果的逆序即为中缀表达式对应的后缀表达式
代码
```java package com.sgg.datastructure.stack;
import cn.hutool.core.util.StrUtil;
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Stack;
public class 表达式 {
public static List
private static boolean isPriority(String str, String peek) {return getScore(str) > getScore(peek);}private static int getScore(String str) {switch (str) {case "+":case "-":return 1;case "*":case "/":return 2;default:throw new RuntimeException("参数有误");}}public static boolean isOperate(String str) {List<String> list = Arrays.asList("+", "-", "*", "/");return list.contains(str);}public static List<String> toInfixExpressionList(String expression) {List<String> result = new ArrayList<>();String ele = "";String[] split = StrUtil.split(expression, 1);for (String raw : split) {if (StrUtil.isNumeric(raw)) {ele += raw;} else {if (StrUtil.isNotBlank(ele)) {result.add(ele);ele = "";}result.add(raw);}}if (StrUtil.isNotBlank(ele)) {result.add(ele);}return result;}public static void main(String[] args) {String middle = "1+((2+3)*41)-20";List<String> ex = 中缀转后缀(middle);System.out.println(ex);System.out.println(后缀表达式计算器.cal(StrUtil.join(StrUtil.SPACE, ex)));}
}
总结
思路挺难的,是数学和计算机思维,主要学应用和思想!
