4.栈
1、定义
- 栈是一个先入后出的有序列表
- 栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。允许插入和删除的一端,为变化的一端,称为栈顶,另一端为固定的一端,称为栈底
- 最先放入的元素在栈底,且最后出栈。最后放入的元素在栈顶,且最先出栈
2、应用场景
- 子程序递归调用。如JVM中的虚拟机栈
- 表达式转换(中缀转后缀)与求值
- 二叉树的遍历
- 图的深度优先遍历
3、实现
3.1 用数组实现
思路
- 定义top表示栈顶,初始值为-1
- 入栈的操作,先让top++,再放入数组
- 出栈操作,先取出元素,在让top–
- top == -1时,栈空
- top == maxSize-1时,栈满
代码
public class Demo1 {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(5);
//压栈
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
//出栈
System.out.println(stack.pop());
}
}
class ArrayStack {
private final int maxSize;
int[] stack;
private int top;
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new int [this.maxSize];
top = -1;
}
private boolean isEmpty() {
return top == -1;
}
private boolean isFull() {
return top == maxSize-1;
}
public void push(int i) {
if(isFull()) {
throw new StackOverflowError("栈满");
}
//压栈
top++;
stack[top] = i;
}
public int pop() {
if(isEmpty()) {
throw new EmptyStackException();
}
int retNum = stack[top];
top--;
return retNum;
}
}
4、应用
4.1 表达式求值
思路
- 准备一个索引index来帮助我们遍历表达式
- 如果index位置上的元素是一个数字,就直接入栈
- 如果index位置上的元素是一个符号
- 如果符号栈为空,直接入栈
- 如果符号栈不为空
- index位置上的符号的优先级小于或等于栈顶符号的优先级,则弹出两个数栈中的元素和符号栈中的一个符号,并且进行计算。将运算结果放入数栈中,并将index位置上的符号压入符号栈
- index位置上的符号的优先级大于符号栈栈顶符号的优先级,则将该符号压入符号栈
- 当表达式遍历完毕后,就弹出数栈中的2个数字和符号栈中的1个符号进行运算,并将运行结果入栈
- 最终数栈中只有一个值,这个值便是运算结果
- 注意:
- 读取的是字符,所以存入数字前需要减去0的ASCII码
- 如果数字是多位数,需要一直读,读到下一位不是数字为止,然后将读到的字符进行拼接,然后一起压入数栈
代码
存在的问题:因为栈是用的整型数组,所以计算除法的时候,无法转化成double
public class Demo2 {
public static void main(String[] args) {
String formula = "12+3*15+3/3";
//索引,用来读取字符串中的元素
int index = 0;
//保存读取到的数字和符号
int number1 = 0;
int number2 = 0;
int thisChar = ' ';
//用于拼接数字
StringBuilder spliceNumber = new StringBuilder();
//数栈和符号栈
ArrayStack2 numberStack = new ArrayStack2(10);
ArrayStack2 operationStack = new ArrayStack2(10);
//保存运算结果
int result;
//开始读取字符串中的元素
for (index = 0; index < formula.length(); index++) {
thisChar = formula.charAt(index);
if (operationStack.isOperation(thisChar)) {
if(operationStack.comparePriority(thisChar)) {
operationStack.push(thisChar);
} else {
int popChar = operationStack.pop();
number2 = numberStack.pop();
number1 = numberStack.pop();
//获得运算结果
result = operationStack.calculation(number1, number2, popChar);
operationStack.push(thisChar);
numberStack.push(result);
}
} else {
//如果是数字,就一直读取
while(thisChar>='0' && thisChar<='9') {
//可能该数字为多位数,所以不能只存入一位数字
spliceNumber.append(thisChar - '0');
System.out.println("拼接字符换 " + spliceNumber);
index++;
//如果已经读了最后一个数字了,就停下来
if(index >= formula.length()) {
break;
}
thisChar = formula.charAt(index);
}
int number = Integer.parseInt(spliceNumber.toString());
numberStack.push(number);
//初始化spliceNumber
spliceNumber = new StringBuilder();
index--;
}
}
while(!operationStack.isEmpty()) {
int popChar = operationStack.pop();
number2 = numberStack.pop();
number1 = numberStack.pop();
//获得运算结果
result = operationStack.calculation(number1, number2, popChar);
numberStack.push(result);
}
System.out.println(numberStack.pop());
}
}
class ArrayStack2 {
private final int maxSize;
int[] stack;
private int top;
public ArrayStack2(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
top = -1;
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == maxSize - 1;
}
public void push(int i) {
if (isFull()) {
throw new StackOverflowError("栈满");
}
//压栈
top++;
stack[top] = i;
}
public int pop() {
if (isEmpty()) {
throw new EmptyStackException();
}
int retNum = stack[top];
top--;
return retNum;
}
public void traverse() {
for(int thiChar : stack) {
System.out.println(thiChar);
}
}
/**
* 判断符号的优先级
*
* @param operation 传入运算符
* @return 返回优先级
*/
public int getPriority(int operation) {
if (operation == '*' || operation == '/') {
return 2;
} else if (operation == '+' || operation == '-') {
return 1;
} else if (operation >= '0' && operation <= '9') {
return 0;
} else {
return -1;
}
}
/**
* 比较栈顶元素和传入字符的优先级大小
*
* @param operation 传入字符
* @return true则是传入字符优先级大于栈顶字符,false反之
*/
public boolean comparePriority(int operation) {
if (isEmpty()) {
return true;
} else {
int priority1 = getPriority(operation);
int priority2 = getPriority(stack[top]);
return priority1 > priority2;
}
}
/**
* 判断该位置是不是一个符号
*
* @param operation 该位置的符号
* @return 判断结果
*/
public boolean isOperation(int operation) {
return operation == '*' || operation == '/' || operation == '-' || operation == '+';
}
/**
* @param number1 第一个运算的数字
* @param number2 第二个运算的数字
* @param operation 运算符
* @return
*/
public int calculation(int number1, int number2, int operation) {
switch (operation) {
case '+':
return number1+number2;
case '-':
return number1-number2;
case '*':
return number1*number2;
case '/':
return number1/number2;
default:
System.out.println(operation);
throw new RuntimeException("符号读取错误!");
}
}
}
结果
58Copy
5、中缀转后缀
后缀表达式运算方法
-
从左向右读取表达式
- 遇到数字就压入栈中
- 遇到运算符就弹出栈顶和次顶元素。用次顶元素 运算符 栈顶元素,并将运算结果压入栈中,直到栈为空,最终结果就是运算结果
5.1 设计
中缀表达式转后缀表达式
- 从左向右读取中缀表达式,并且创建栈s和队列q
- 如果读到的元素的数字,就直接入队放入q中
- 如果读到的是运算符(运算符判定)
- 如果s为空,则将该运算符压入s
- 如果s不为空
- 如果该运算符为左括号,则直接压入s
- 如果该运算符为右括号,则将s中的元素依次出栈并入队到q中,直到遇见左括号为止(括号不放入q中)
- 如果该运算符的优先级高于s栈顶的运算符,则将该元素压入s
- 如果该运算符的优先级小于等于s栈顶的运算符,则弹出s栈顶的元素,并将其放入q中,该运算符重新判定入栈操作(运算符判定步骤)
- 如果中缀表达式已经读取完毕,则将s中的元素依次出栈,放入q中
- q中的元素依次出队,该顺序即为后缀表达式
代码
/**
* @author Chen Panwen
* @data 2020/6/17 21:07
*/
public class Demo4 {
static Queue<String> queue = new LinkedList<>();
static Stack<String> stack = new Stack<>();
public static void main(String[] args) {
//中缀表达式,加上空格,方便取出
String infixExpression = "1 + ( ( 2 + 3 ) * 4 ) - 5";
String[] expressionArr = infixExpression.split(" ");
//用来保存该运算符的类型
int type;
//取出的字符串
String element;
//弹出栈的字符串
String stackEle;
for(int i=0; i<expressionArr.length; i++) {
element = expressionArr[i];
type = judgeOperator(element);
if(type == 0) {
//数字,直接入队
queue.add(element);
}else if(type == 1) {
//左括号,直接压栈
stack.push(element);
}else if(type == 3) {
//如果右括号,弹出栈顶元素,直到遇见左括号位置再停下来
do {
stackEle = stack.pop();
if(stackEle.equals("(")) {
break;
}
queue.add(stackEle);
//弹出栈中的左括号
}while (!stackEle.equals("("));
}else if(type == 2) {
if(stack.isEmpty()) {
//如果栈为空,直接入栈
stack.push(element);
continue;
}
int priority1 = getPriority(element);
//获得栈顶元素,并判断其优先级
stackEle = stack.peek();
int priority2 = getPriority(stackEle);
if(priority2 == 0) {
//为左括号,运算符直接入栈
stack.push(element);
}else if(priority1 > priority2) {
//该运算符优先级高于栈顶元素优先级,则入栈
stack.push(element);
}else {
stackEle = stack.pop();
queue.add(stackEle);
//重新判断该运算符
i--;
}
}
}
//把最后一个元素出栈并入队
stackEle = stack.pop();
queue.add(stackEle);
//保存队列长度,因为出队过程中队列的长度会被改变
int length = queue.size();
for(int i=0; i<length; i++) {
element = queue.remove();
System.out.print(element);
}
}
/**
* 判断该运算符是不是加减乘除
* @param operation 运算符
* @return true则该运算符为加减乘除
*/
public static boolean firstJudge(String operation) {
return operation.equals("*") || operation.equals("/") || operation.equals("+") || operation.equals("-");
}
/**
* 判断该字符串的类型
* @param operation 要判断的字符串
* @return 3->右括号 2->加减乘除运算符 1->左括号
*/
public static int judgeOperator(String operation) {
if(operation.equals(")")) {
return 3;
}
if(firstJudge(operation)) {
return 2;
}
if(operation.equals("(")) {
return 1;
} else {
return 0;
}
}
/**
* 判断运算符优先级
* @param operator 要判断的运算符
* @return 2代表乘除,1代表加减,0代表左括号
*/
public static int getPriority(String operator) {
if(operator.equals("*") || operator.equals("/")) {
return 2;
}else if(operator.equals("+") || operator.equals("-")) {
return 1;
} else {
return 0;
}
}
}
结果
123+4*+5-