栈
1.栈的定义
- 栈的英文为(stack)
- 栈是一个先入后出(FILO-First In Last Out)的有序列表。
- 栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。允许插入和删除的
一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)。 - 根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元
素最先删除,最先放入的元素最后删除 - 图解方式说明出栈(pop)和入栈(push)的概念
2.栈实现综合计算器
- 请计算表达式:[722-5+1-5+3-3] 的值
- 请问: 计算机底层是如何运算得到结果的? 注意不是简单的把算式列出运算,因为我们看这个算式 7 * 2 * 2 - 5,但是计算机怎么理解这个算式的
- 对计算机而言, 它接收到的就是一个字符串, 我们讨论的是这个问题:栈
2.后缀表达式
- 后缀表达式又称逆波兰表达式,与前缀表达式相似,只是运算符位于操作数之后
- 中缀表达式举例说明: (3+4)×5-6 对应的后缀表达式就是 3 4 + 5 × 6 –
- 再比如:
正常的表达式 | 逆波兰表达式 |
---|---|
a+b | a b + |
a+(b-c) | a b c - + |
a+(b-c)*d | a b c – d * + |
a+d*(b-c) | a d b c - * + |
a=1+3 | a 1 3 + = |
3.1 逆波兰计算器
我们完成一个逆波兰计算器,要求完成如下任务:
- 输入一个逆波兰表达式(后缀表达式),使用栈(Stack), 计算其结果
- 支持小括号和多位数整数,因为这里我们主要讲的是数据结构,因此计算器进行简化,只支持对整数的计算。
- 思路分析
例如: (3+4)×5-6 对应的后缀表达式就是 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,由此得出最终结果
3.2 代码思路
- 计算后缀表达式无需考虑运算符优先级问题,所以只需要一个数栈即可
- 分为两种情况:
- 遇到数:压入数栈
- 遇到运算符:从数栈中弹出两个数,进行计算,计算结果压入数栈
- 何时计算完成?处理完表达式就代表计算完成
代码实现
- 出栈的两个数:num2 和 num1
- num2 先出栈,所以 num2 是减数或除数
- num1 后出栈,所以 num1 是被减数或被除数
public class PolandNotation {
public static void main(String[] args) {
//先定义给逆波兰表达式
// 4 * 5 - 8 + 60 + 8 / 2 => 4 5 * 8 - 60 + 8 2 / +
//说明为了方便,逆波兰表达式 的数字和符号使用空格隔开
String suffixExpression = "4 5 * 8 - 60 + 8 2 / +"; // 76
//思路
//1. 先将逆波兰表达式 => 放到ArrayList中
//2. 将 ArrayList 传递给一个方法,遍历 ArrayList 配合栈 完成计算
List<String> list = getListString(suffixExpression);
System.out.println("rpnList=" + list);
int res = calculate(list);
System.out.println("计算的结果是=" + res);
}
//将一个逆波兰表达式, 依次将数据和运算符 放入到 ArrayList中
public static List<String> getListString(String suffixExpression) {
//将 suffixExpression 分割
String[] split = suffixExpression.split(" ");
List<String> list = new ArrayList<String>();
for(String ele: split) {
list.add(ele);
}
return list;
}
//完成对逆波兰表达式的运算
/*
* 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 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("运算符有误");
}
//把res 入栈
stack.push("" + res);
}
}
//最后留在stack中的数据是运算结果
return Integer.parseInt(stack.pop());
}
}
4.中缀表达式转后缀表达式
4.1举例说明
- 举例说明:将中缀表达 式“1+((2+3)×4)-5”转换为后缀表达式的过程如下
- 因此结果为:“1 2 3 + 4 × + 5 –”
4.2 代码实现
- 将中缀表达式转为对应的 List :将数字和运算符分开,存储在 List 对象中
public class PolandNotation {
public static void main(String[] args) {
// 完成将一个中缀表达式转成后缀表达式的功能
// 说明
// 1. 1+((2+3)×4)-5 => 转成 1 2 3 + 4 × + 5 –
// 2. 因为直接对str 进行操作,不方便,因此 先将 "1+((2+3)×4)-5" =》 中缀的表达式对应的List
// 即 "1+((2+3)×4)-5" => ArrayList [1,+,(,(,2,+,3,),*,4,),-,5]
// 3. 将得到的中缀表达式对应的List => 后缀表达式对应的List
// 即 ArrayList [1,+,(,(,2,+,3,),*,4,),-,5] =》 ArrayList [1,2,3,+,4,*,+,5,–]
String expression = "1+((2+3)*4)-5";// 注意表达式
List<String> infixExpressionList = toInfixExpressionList(expression);
System.out.println("中缀表达式对应的List=" + infixExpressionList); // ArrayList [1,+,(,(,2,+,3,),*,4,),-,5]
List<String> suffixExpreesionList = parseSuffixExpreesionList(infixExpressionList);
System.out.println("后缀表达式对应的List" + suffixExpreesionList); // ArrayList [1,2,3,+,4,*,+,5,–]
System.out.printf("expression=%d", calculate(suffixExpreesionList)); // ?
}
// 方法:将 中缀表达式转成对应的List
// s="1+((2+3)×4)-5";
public static List<String> toInfixExpressionList(String s) {
// 定义一个List,存放中缀表达式 对应的内容
List<String> ls = new ArrayList<String>();
int i = 0; // 这时是一个指针,用于遍历 中缀表达式字符串
String str; // 对多位数的拼接
char c; // 每遍历到一个字符,就放入到c
do {
// 如果c是一个非数字,我需要加入到ls
if ((c = s.charAt(i)) < 48 || (c = s.charAt(i)) > 57) {
ls.add("" + c);
i++; // i需要后移
} else { // 如果是一个数,需要考虑多位数
str = ""; // 先将str 置成"" '0'[48]->'9'[57]
while (i < s.length() && (c = s.charAt(i)) >= 48 && (c = s.charAt(i)) <= 57) {
str += c;// 拼接
i++;
}
ls.add(str);
}
} while (i < s.length());
return ls;// 返回
}
// 即 ArrayList [1,+,(,(,2,+,3,),*,4,),-,5] =》 ArrayList [1,2,3,+,4,*,+,5,–]
// 方法:将得到的中缀表达式对应的List => 后缀表达式对应的List
public static List<String> parseSuffixExpreesionList(List<String> ls) {
// 定义两个栈
Stack<String> operStack = new Stack<String>(); // 符号栈
// 说明:因为tempList 这个栈,在整个转换过程中,没有pop操作,而且后面我们还需要逆序输出
// 因此比较麻烦,这里我们就不用 Stack<String> 直接使用 List<String> tempList
// Stack<String> tempStack = new Stack<String>(); // 储存中间结果的栈tempStack
List<String> tempList = new ArrayList<String>(); // 储存中间结果的tempList
// 遍历ls
for (String item : ls) {
if (item.matches("\\d+")) { // 如果是一个数,加入tempList
tempList.add(item);
} else if (item.equals("(")) { // 如果是 ( ,则直接入operStack
operStack.push(item);
} else if (item.equals(")")) { // 如果是 ) ,则将括号内的值算出,并压入 tempList)
// 如果是右括号“)”,则依次弹出operStack栈顶的运算符,并压入tempList,直到遇到左括号为止,此时将这一对括号丢弃
while (!operStack.peek().equals("(")) {
tempList.add(operStack.pop());
}
operStack.pop();// !!! 将 ( 弹出 s1栈, 消除小括号
} else { // 否则比较当前运算符和栈顶运算符优先级
// 当item的优先级小于等于operStack栈顶运算符,
// 将operStack栈顶的运算符弹出并加入到tempList中,再次转到(4.1)与operStack中新的栈顶运算符相比较
// 问题:我们缺少一个比较优先级高低的方法
while (operStack.size() != 0 && Operation.getValue(operStack.peek()) >= Operation.getValue(item)) {
tempList.add(operStack.pop());
}
// 还需要将item压入栈
operStack.push(item);
}
}
// 将operStack中剩余的运算符依次弹出并加入tempList
while (operStack.size() != 0) {
tempList.add(operStack.pop());
}
return tempList; // 注意因为是存放到List, 因此按顺序输出就是对应的后缀表达式对应的List
}
// 完成对逆波兰表达式的运算
/*
* 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 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("运算符有误");
}
// 把res 入栈
stack.push("" + res);
}
}
// 最后留在stack中的数据是运算结果
return Integer.parseInt(stack.pop());
}
}
//编写一个类 Operation 可以返回一个运算符 对应的优先级
class Operation {
private static int LEFT_BRACKET = 0;
private static int ADD = 1;
private static int SUB = 1;
private static int MUL = 2;
private static int DIV = 2;
// 写一个方法,返回对应的优先级数字
public static int getValue(String operation) {
int result = 0;
switch (operation) {
case "(":
result = LEFT_BRACKET;
break;
case "+":
result = ADD;
break;
case "-":
result = SUB;
break;
case "*":
result = MUL;
break;
case "/":
result = DIV;
break;
default:
System.out.println("不存在该运算符" + operation);
break;
}
return result;
}
}