1、栈的一个实际需求
请输入一个表达式
计算式:[ 722-5+1-5+3-3 ] 点击计算【如下图】
请问:计算机底层是如何运算得到的结果的?
2、栈的介绍
- 栈的英文为(Stack)
- 栈是一个
先进后出
的有序链表 - 栈是限制线性表中元素插入和删除只能在线性表的同一端进行操作。允许插入和删除的一端,为变化的一端,称为
栈顶(top)
,另一端为固定的一端,称为栈底(bottom)
- 根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入放入元素最先删除,最先放入放入元素最后删除
- 图解说明出栈(pop)和入栈(push)的概念
3、栈的应用场景
🧨 子程序的调用:在跳往子程序前,会先将下个指令的地址存到堆栈中,直到子程序执行完毕后再将地址取出,以回到原来的程序中。
🧨 处理递归调用:和子程序的调用类似,只是除了存储下一个指令的地址外,也将参数、区域变量等数据存储到堆栈中。
🧨 表达式的交换【中缀表达式转后缀表达式】与求值(实际解决)
🧨 二叉树的遍历
🧨 图形的深度优先搜索法
4、栈的快速入门
🎊 用数组模拟栈的使用,由于栈是一种有序列表,当然可以使用数组的结构来存储栈的数据内容,下面就使用数组模拟栈的入栈和出栈操作
🎊 实现思路分析示意图:
💻 代码实现
package com.yao.stack;
public class ArrayStackDemo {
public static void main(String[] args) {
//测试栈
ArrayStack stack = new ArrayStack(5);
stack.push(3);
stack.push(5);
stack.push(1);
stack.push(8);
stack.push(2);
System.out.println(stack.pop());
stack.list();
}
}
class ArrayStack{
private int maxSize; //栈的最大空间
private int [] stack; //数组模拟栈,数据就放在数组中
private int top = -1; //栈顶
public ArrayStack(int maxSize){
this.maxSize = maxSize;
stack = new int[maxSize];
}
/**
* 判断栈满
*/
public boolean isFull(){
return top == maxSize - 1;
}
/**
* 判断栈空
*/
public boolean isEmpty(){
return top == -1;
}
/**
* 入栈操作
*/
public void push(int value){
//判断栈是否满
if (isFull()){
return;
}
top ++;
stack[top] = value;
}
/**
* 出栈操作
*/
public int pop(){
//判断栈是否空
if (isEmpty()){
throw new RuntimeException("栈已经空....");
}
int temp = stack[top];
top -- ;
return temp;
}
/**
* 栈的遍历
*/
public void list(){
//判断栈空
if (isEmpty()){
return;
}
for (int i = top; i >= 0; i--) {
System.out.printf("stack[%d]=%d\n",i,stack[i]);
}
}
}
🎊 使用链表模拟栈
🎊 思路分析:
- 入栈操作:利用一个辅助指针指向头结点,就判断辅助指针的下一个节点是否为空,如果为空 temp.next = node, 否则一直移动辅助指针。
- 出栈操作:就是逆序输出链表,可以查看此处:链表逆序输出
💻 代码实现
package com.yao.stack;
import java.util.Stack;
public class LinkedListStackDemo {
public static void main(String[] args) {
//测试
LinkedListStack stack = new LinkedListStack();
Node node1 = new Node("2");
Node node2 = new Node("5");
Node node3 = new Node("9");
Node node4 = new Node("6");
stack.push(node1);
stack.push(node2);
stack.push(node3);
stack.push(node4);
stack.pop(stack.getHead());
}
}
/**
* 创建含有头节点的单链表
*/
class LinkedListStack{
//声明头结点
public Node head = new Node("");
/**
* 获取头结点
*/
public Node getHead(){
return this.head;
}
/**
* 判断栈空
*/
public boolean isEmpty(){
return head.next == null;
}
/**
* 进栈
*/
public void push(Node node){
//声明辅助指针
Node temp = head;
while (temp.next != null){
temp = temp.next;
}
temp.next = node;
}
/**
* 出栈,相当于逆序输出链表
*/
public void pop(Node head){
if (head.next == null){
System.out.println("栈空....");
}
Stack<Node> nodes = new Stack<>();
Node temp = head.next;
while (temp != null){
nodes.push(temp);
temp = temp.next;
}
while (nodes.size() > 0){
System.out.println(nodes.pop());
}
}
}
class Node{
public String value;
public Node next;
public Node(String value){
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
}
5、栈实现综合计算器(中缀表达式)
前缀表达式、中缀表达式、后缀表达式都是对表达式的记法,也被称为前缀记法、中缀记法和后缀记法。它们之间的区别在于运算符相对与操作数的位置不同:前缀表达式的运算符位于与其相关的操作数之前;中缀和后缀同理。
举例:
(3 + 4) × 5 - 6 中缀表达式
- × + 3 4 5 6 前缀表达式
3 4 + 5 × 6 - 后缀表达式
中缀表达式是一种通用的算术或逻辑公式表示方法,操作符以中缀形式处于操作数的中间。中缀表达式是人们常用的算术表示方法。
虽然人的大脑很容易理解与分析中缀表达式,但对计算机来说中缀表达式却是很复杂的,因此计算表达式的值时,通常需要先将中缀表达式转换为前缀或后缀表达式,然后再进行求值。对计算机来说,计算前缀或后缀表达式的值非常简单。
前缀表达式(前缀记法、波兰式)
前缀表达式的运算符位于操作数之前。
后缀表达式(后缀记法、逆波兰式)
后缀表达式的运算符位于操作数之后。
🎈 思路分析(图解)
💻 代码实现
package com.yao.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 num1;
int num2;
int oper;
int res;
int index = 0;
char temp;
String keepNum = ""; //用于拼接多位数
do {
temp = expression.substring(index, index + 1).charAt(0); // 获取表达式字符存储在temp中
//判断还是否是运算符
if (operStack.isOper(temp)) { //如果是运算符
//判断符号栈中是否为空
if (operStack.isEmpty()) {
//直接入栈
operStack.push(temp);
} else {
//进行操作符优先级运算
//当前运算符的优先级小于等于栈顶的优先级
if (operStack.priority(temp) <= operStack.priority(operStack.peek())) {
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.calculate(num1, num2, oper);
numStack.push(res);
operStack.push(temp);
} else {
//当前运算符的优先级大于栈顶的优先级
operStack.push(temp);
}
}
} else {
//如果是多位数,不能直接入栈,需要判断下一个字符是不是符号
//numStack.push(Integer.parseInt(String.valueOf(temp)));
//因此需要定一个字符串变量进行拼接
//处理多位数
keepNum += temp;
//判断下一个字符是不是数字,如果是就继续扫描,否则就入数栈
//只是看后面一位,不是 index ++
//如果当前位置是最后一个位置
if (index == expression.length() - 1){
numStack.push(Integer.parseInt(keepNum));
}else {
if (operStack.isOper(expression.substring(index+1,index+2).charAt(0))){
//如果后一位是运算符,则入栈 keepNum = "1" 或 "123"
numStack.push(Integer.parseInt(keepNum));
//重要!!!,清空keepNum
keepNum = "";
}
}
}
index++;
} while (index < expression.length());
while (!operStack.isEmpty()) {
//如果符号栈为空运算结束
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.calculate(num1, num2, oper);
numStack.push(res);
}
System.out.printf("表达式 %s = %d",expression,numStack.pop());
}
}
/**
* 初始化一个栈
*/
class ArrayStack2{
private final int maxSize;
private final int [] array;
private int top = -1;
public ArrayStack2(int maxSize){
this.maxSize = maxSize;
array = new int[maxSize];
}
/**
* 判断栈空
*/
public boolean isEmpty(){
return top == -1;
}
/**
* 判断栈满
*/
public boolean isFull(){
return top == maxSize-1;
}
/**
* 入栈操作
*/
public void push(int num){
if (isFull()){
return;
}
top++;
array[top] = num;
}
/**
* 出栈操作
*/
public int pop(){
if (isEmpty()){
return 0;
}
int temp = array[top];
top -- ;
return temp;
}
/**
* 遍历栈中元素
*/
public void list(){
if (isEmpty()){
return;
}
for (int i = top; i >= 0 ; i--) {
System.out.printf("栈中元素array[%d] = %d",i,array[i]);
}
}
/**
* 查看栈顶的元素
*/
public int peek(){
return array[top];
}
/**
* 返回运算符的优先级,数字越大优先级越高
*/
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 calculate(int num1, int num2, int oper){
int result = 0; //存放计算结果
switch (oper) {
case '*':
result = num1 * num2;
break;
case '/':
result = num2 / num1;
break;
case '+':
result = num1 + num2;
break;
case '-':
result = num2 - num1;
break;
}
return result;
}
}
6、逆波兰计算器
- 输入一个逆波兰表达式(后缀表达式),使用栈,计算其结果
- 支持小括号和多位数运算
- 思路分析
💻 代码实现
package com.yao.stack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
public class PolandNotation {
public static void main(String[] args) {
//先定义逆波兰表达式
//为了方便观察,运算符与数字之间使用空格隔开
String suffixExpression = "30 4 + 5 * 6 - ";
//1.将后缀表达式放入到list集合中
List<String> listString = getListString(suffixExpression);
//2.遍历list 配合栈完成计算
int calculate = getCalculate(listString);
System.out.println("计算结果:"+calculate);
}
//将一个逆波兰表达式,依次将运算符与数据放入到 ArrayList 中
public static List<String> getListString(String suffixExpression){
//将后缀表达式进行分割,存储到list中
String[] s = suffixExpression.split(" ");
ArrayList<String> strings = new ArrayList<>();
Collections.addAll(strings, s);
return strings;
}
//完成对逆波兰表达式计算
public static int getCalculate(List<String> list){
//创建一个栈
Stack<String> stack = new Stack<>();
//遍历list
list.forEach(item -> {
//使用正则表达式取出数
if (item.matches("\\d+")){
//直接入栈
stack.push(item);
}else {
//pop出两个数,并运算,再入栈
int num2 = Integer.parseInt(stack.pop());
int num1 = Integer.parseInt(stack.pop());
int res = 0;
switch (item) {
case "+":
res = num1 + num2;
break;
case "-":
res = num1 - num2;
break;
case "*":
res = num1 * num2;
break;
case "/":
res = num1 / num2;
break;
}
//把res入栈
stack.push(String.valueOf(res));
}
});
//最后存在栈中的数据就是计算结果
return Integer.parseInt(stack.pop());
}
}
/**
* 编写一个 Operation 类,可以返回一个运算符的优先级
*/
class Operation{
private static final int ADD = 1;
private static final int SUB = 1;
private static final int MUL = 2;
private static final int DIV = 2;
public static int preority(String str){
int res = 0;
switch (str) {
case "+":
res = ADD;
break;
case "-":
res = SUB;
break;
case "*":
res = MUL;
break;
case "/":
res = DIV;
break;
default:
break;
}
return res;
}
}
7、中缀表达式转为后缀表达式
后缀表达式合适计算机进行运算,但是人却不容易看出来,尤其是表达式很长的情况下,因此在开发中需要将中缀表达式转为后缀表达式。
具体步骤如下:
-
初始化两个栈:运算符栈 s1 和存储中间结果的栈 s2
-
从左至右扫描中缀表达式
-
遇到操作数时,将其压入 s2
-
遇到运算符时,比较其与 s1 栈顶运算符的优先级
1)如果 s1 为空,或栈顶运算符为左括号“(“,则直接将次运算符入栈
2)否则,若优先级比栈顶运算符高,也将其压入 s1
3)否则,将 s1 栈顶的运算符弹出并压入 s2 中,再次转到 (4-1) 步骤与 s1 中新的栈顶运算符比较; -
遇到括号时:
1)如果是左括号“(”,则直接压入 s1
2) 如果是右括号“)”,则依次弹出 s1 栈顶运算符,并压入 s2 中,直到遇到左括号为止,此时将这一对括号丢弃 -
将 s1 中剩余的运算符一次弹出并压入 s2
-
一次弹出 s2 中元素并输出,结果的逆序即为后缀表达式
举例说明
将中缀表达式" 1+((2+3)*4)-5 "转换为后缀表达式
因此 结果为: 1 2 3 + 4 * + 5 -
💻 代码实现
/*
将一个中缀表达式转化为后缀表达式
1.现将中缀表达式存储在 List 集合中
toInfixList(expression);
2.将 List 集合中的元素进行遍历,依次放入栈中,转成后缀表达式
toSuffixExpression(List<String> list)
*/
String expression = "10+((20+3)*4)-5";
List<String> strings = toInfixList(expression);
System.out.println(strings);
List<String> strings1 = toSuffixExpression(strings);
System.out.println(strings1);
//将中缀表达式存储到List中
public static List<String> toInfixList(String expression){
ArrayList<String> strings = new ArrayList<>();
int index = 0;
char ch;
String temp;
do {
if ((ch=expression.charAt(index)) < 48 || (ch=expression.charAt(index)) > 57){
//说明是非数值,直接放进list
strings.add(String.valueOf(ch));
index ++;
}else { //说明是数字
temp = "";
while (index < expression.length() && (ch = expression.charAt(index)) >= 48 && (ch = expression.charAt(index)) <= 57){
temp += ch;
index ++;
}
strings.add(temp);
}
}while (index < expression.length());
return strings;
}
/**
* 中缀表达式转后缀表达式
*/
public static List<String> toSuffixExpression(List<String> list){
//声明两个栈
Stack<String> s1 = new Stack<>();
//由于 s2 栈,没有出栈操作,值是往里加,所以可以使用 List 代替
List<String> s2 = new ArrayList<>();
//遍历
list.forEach(item ->{
//如果是数字,将其添加到 s2
if (item.matches("\\d+")){
s2.add(item);
}else if ("(".equals(item)){ //如果是运算符,并且是左括号"(",直接压入到 s1
s1.push(item);
}else if (")".equals(item)){ //如果是右括号“)”,则依次弹出 s1 中栈顶的运算符,并添加到 s2 中,直到遇到左括号"("为止,此时将这一对括号丢弃
while (!s1.peek().equals("(")){
s2.add(s1.pop());
}
s1.pop(); //将s1中的括号丢弃
}else { //遇到操作符
while (!s1.isEmpty() && Operation.preority(item) <= Operation.preority(s1.peek())){
//如果item 的优先级比栈顶运算符的优先级低,则将s1中的运算符添加到s2中,再次与s1中新的栈顶元素进行比较
s2.add(s1.pop());
// //如果 s1 为空或者栈顶元素为左括号"(",直接入栈
// if (s1.isEmpty() || s1.peek().equals("(")){
// s1.push(item);
// break;
// }
}
//还需要将 item 压入到 s1
s1.push(item);
}
});
//将 s1 中剩余运算符依次弹出,并加入到s2中
while (!s1.isEmpty()){
s2.add(s1.pop());
}
return s2;
}
8、逆波兰计算器完整版
完整版逆波兰计算器,功能包括
- 支持 + - * /
- 多位数,支持小数
- 兼容处理,过滤任何空白字符,包括空格、制表符、换行
💻 代码实现:
package com.yao.stack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
import java.util.regex.Pattern;
public class ReversePolishMultiCalc {
/**
* 匹配 + - * / ( ) 运算符
*/
static final String SYMBOL = "\\+|-|\\*|/|\\(|\\)";
static final String LEFT = "(";
static final String RIGHT = ")";
static final String ADD = "+";
static final String MINUS= "-";
static final String TIMES = "*";
static final String DIVISION = "/";
/**
* 加減 + -
*/
static final int LEVEL_01 = 1;
/**
* 乘除 * /
*/
static final int LEVEL_02 = 2;
/**
* 括号
*/
static final int LEVEL_HIGH = Integer.MAX_VALUE;
static Stack<String> stack = new Stack<>();
static List<String> data = Collections.synchronizedList(new ArrayList<String>());
/**
* 去除所有空白符
* @param s
* @return
*/
public static String replaceAllBlank(String s ){
// \\s+ 匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v]
return s.replaceAll("\\s+","");
}
/**
* 判断是不是数字 int double long float
* @param s
* @return
*/
public static boolean isNumber(String s){
Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
return pattern.matcher(s).matches();
}
/**
* 判断是不是运算符
* @param s
* @return
*/
public static boolean isSymbol(String s){
return s.matches(SYMBOL);
}
/**
* 匹配运算等级
* @param s
* @return
*/
public static int calcLevel(String s){
if("+".equals(s) || "-".equals(s)){
return LEVEL_01;
} else if("*".equals(s) || "/".equals(s)){
return LEVEL_02;
}
return LEVEL_HIGH;
}
/**
* 匹配
* @param s
* @throws Exception
*/
public static List<String> doMatch (String s) throws Exception{
if(s == null || "".equals(s.trim())) throw new RuntimeException("data is empty");
if(!isNumber(s.charAt(0)+"")) throw new RuntimeException("data illeagle,start not with a number");
s = replaceAllBlank(s);
String each;
int start = 0;
for (int i = 0; i < s.length(); i++) {
if(isSymbol(s.charAt(i)+"")){
each = s.charAt(i)+"";
//栈为空,(操作符,或者 操作符优先级大于栈顶优先级 && 操作符优先级不是( )的优先级 及是 ) 不能直接入栈
if(stack.isEmpty() || LEFT.equals(each)
|| ((calcLevel(each) > calcLevel(stack.peek())) && calcLevel(each) < LEVEL_HIGH)){
stack.push(each);
}else if( !stack.isEmpty() && calcLevel(each) <= calcLevel(stack.peek())){
//栈非空,操作符优先级小于等于栈顶优先级时出栈入列,直到栈为空,或者遇到了(,最后操作符入栈
while (!stack.isEmpty() && calcLevel(each) <= calcLevel(stack.peek()) ){
if(calcLevel(stack.peek()) == LEVEL_HIGH){
break;
}
data.add(stack.pop());
}
stack.push(each);
}else if(RIGHT.equals(each)){
// ) 操作符,依次出栈入列直到空栈或者遇到了第一个)操作符,此时)出栈
while (!stack.isEmpty() && LEVEL_HIGH >= calcLevel(stack.peek())){
if(LEVEL_HIGH == calcLevel(stack.peek())){
stack.pop();
break;
}
data.add(stack.pop());
}
}
start = i ; //前一个运算符的位置
}else if( i == s.length()-1 || isSymbol(s.charAt(i+1)+"") ){
each = start == 0 ? s.substring(start,i+1) : s.substring(start+1,i+1);
if(isNumber(each)) {
data.add(each);
continue;
}
throw new RuntimeException("data not match number");
}
}
//如果栈里还有元素,此时元素需要依次出栈入列,可以想象栈里剩下栈顶为/,栈底为+,应该依次出栈入列,可以直接翻转整个stack 添加到队列
Collections.reverse(stack);
data.addAll(new ArrayList<>(stack));
System.out.println(data);
return data;
}
/**
* 算出结果
* @param list
* @return
*/
public static Double doCalc(List<String> list){
Double d = 0d;
if(list == null || list.isEmpty()){
return null;
}
if (list.size() == 1){
System.out.println(list);
d = Double.valueOf(list.get(0));
return d;
}
ArrayList<String> list1 = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
list1.add(list.get(i));
if(isSymbol(list.get(i))){
Double d1 = doTheMath(list.get(i - 2), list.get(i - 1), list.get(i));
list1.remove(i);
list1.remove(i-1);
list1.set(i-2,d1+"");
list1.addAll(list.subList(i+1,list.size()));
break;
}
}
doCalc(list1);
return d;
}
/**
* 运算
* @param s1
* @param s2
* @param symbol
* @return
*/
public static Double doTheMath(String s1,String s2,String symbol){
Double result ;
switch (symbol){
case ADD : result = Double.valueOf(s1) + Double.valueOf(s2); break;
case MINUS : result = Double.valueOf(s1) - Double.valueOf(s2); break;
case TIMES : result = Double.valueOf(s1) * Double.valueOf(s2); break;
case DIVISION : result = Double.valueOf(s1) / Double.valueOf(s2); break;
default : result = null;
}
return result;
}
public static void main(String[] args) {
//String math = "9+(3-1)*3+10/2";
String math = "12.8 + (2 - 3.55)*4+10/5.0";
try {
doCalc(doMatch(math));
} catch (Exception e) {
e.printStackTrace();
}
}
}
🎃 完结