一,栈
1,简介:
栈:遵循先入后出(FILO) 的有序列表,限制线性表中的元素的插入和删除(取出)只能在同一端进行的一种特殊的线性表;允许插入和删除(取出)的一端称为栈顶;固定的一端为栈底,就如同弹夹;
如下图:
2,分析:
出栈pop:只能从栈顶开始一个一个弹出栈,最后进入的最先弹出;
入栈push:只能从栈顶一个一个压入栈,最先进入的在最底层,最后弹出;
3,数组模拟栈
思路分析:
- 使用数组模拟栈存储;
- 定义top表示栈顶指针,初始化为-1;
- 入栈:当数据加人时,top++,stack[top]=data;
- 出栈:当数据弹出时,int var = stack[top],top–;
代码实现:
package com.stack;
/**
* @param
* @Author: AaNeei
* @Date: 2019/6/14 19:11
* @Description: 游学网
* @throws:
*/
public class StackArray {
private int maxSize;
private int[] stack;
private int top = -1;
public StackArray(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()) {
throw new RuntimeException("栈已满");
}
stack[++top] = value;
}
//出栈
public int pop() {
if (isEmpty()) {
throw new RuntimeException("栈已空");
}
return stack[top--];
}
public void list() {
if (isEmpty()) {
throw new RuntimeException("没有数据");
}
for (int i = top; i >= 0; i--) {
System.out.println("栈数据:" + stack[i]);
}
}
}
4,模拟栈实现表达式计算
4.1,分析
-
定义两个栈,一个存放数字,一个存放符号;
-
通过定义一个index索引,遍历要计算的表达式;
-
如果是数字,就直接入数栈;
-
如果是符号:
4.1. 如果符号栈为空,就直接入栈;
4.2.如果符号栈有值,比较:
①当前操作符优先级 <= 栈中的操作符,就需要从数栈中pop出两个数值,符号栈pop出一个符号,进行运算得到结果入数栈,然后将当前操作符入符号栈;
②如果当前操作符优先级 > 符号栈中的操作符,就直接入符号栈; -
当表达式扫描完成,就顺序的从栈中pop出相应的数值和符号,进行运算;
-
最后的数栈中剩余的一个数值就是运算结果;
4.2,图示
4.3,编码
定义栈:
package com.stack;
/**
* @param
* @Author: AaNeei
* @Date: 2019/6/14 20:17
* @Description: 游学网
* @throws:
*/
public class CalculatorStack {
private int maxSize;
private int[] stack;
private int top = -1;
public CalculatorStack(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()) {
throw new RuntimeException("栈已满");
}
stack[++top] = value;
}
//出栈
public int pop() {
if (isEmpty()) {
throw new RuntimeException("栈已空");
}
return stack[top--];
}
public void list() {
if (isEmpty()) {
throw new RuntimeException("没有数据");
}
for (int i = top; i >= 0; i--) {
System.out.println("栈数据:" + stack[i]);
}
}
//返回栈底值
public int peek() {
return stack[top];
}
//符号优先级
public int priority(int symbol) {
if (symbol == '*' || symbol == '/') {
return 1;
} else if (symbol == '+' || symbol == '-') {
return 0;
} else {
return -1;//不符合
}
}
//是否是符号
public boolean isSymbol(char symbol) {
return symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/';
}
//计算
public int calculate(int num1, int num2, int symbol) {
int result = 0;
switch (symbol) {
case '+':
result = num1 + num2;
break;
case '-':
result = num2 - num1;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num2 / num1;
break;
default:
break;
}
return result;
}
}
定义计算表达式实现(中缀表达式):
package com.stack;
/**
* @param
* @Author: AaNeei
* @Date: 2019/6/14 20:16
* @Description: 游学网
* @throws:
*/
public class Calculator {
//测试
public static void main(String[] args) {
String expression = "1+8*5-9";
int calculate = Calculator.calculate(expression);
System.out.println(expression + "=" + calculate);
}
public static int calculate(String expression) {
int length = expression.length();
//数栈
CalculatorStack numberStack = new CalculatorStack(length);
//符号栈
CalculatorStack symbolStack = new CalculatorStack(length);
//定义变量
int index = 0;
int num1 = 0;
int num2 = 0;
int symbol = 0;
int result = 0;
char ch = ' ';
while (true) {
ch = expression.substring(index, index + 1).charAt(0);
if (symbolStack.isSymbol(ch)) {
//符号栈有值
if (!symbolStack.isEmpty()) {
//当前操作符优先级 <= 栈中的操作符
if (symbolStack.priority(ch) <= symbolStack.priority(symbolStack.peek())) {
//从数栈中pop出两个数值
num1 = numberStack.pop();
num2 = numberStack.pop();
//符号栈pop出一个符号
symbol = symbolStack.pop();
result = numberStack.calculate(num1, num2, symbol);
//运算得到结果入数栈
numberStack.push(result);
//当前操作符入符号栈
symbolStack.push(ch);
}else{
//当前操作符优先级 > 符号栈中的操作符,就直接入符号栈
symbolStack.push(ch);
}
} else {
//符号栈为空,就直接入栈
symbolStack.push(ch);
}
} else {
//数字,就直接入数栈, char-48转数字 见
numberStack.push(ch - 48);
}
index++;
if (index == expression.length()) {
break;
}
}
//当表达式扫描完成,就顺序的从栈中pop出相应的数值和符号,进行运算
while (true) {
if (symbolStack.isEmpty()) {
break;
}
num1 = numberStack.pop();
num2 = numberStack.pop();
symbol = symbolStack.pop();
result = numberStack.calculate(num1, num2, symbol);
//运算得到结果入数栈
numberStack.push(result);
}
//最后的数栈中剩余的一个数值就是运算结果
return numberStack.pop();
}
}
**注:**以上暂只支持个位数的±*/。多位数如何处理?
多位数计算:
当前是数字下一位是符号再入栈;
public static int calculateMore(String expression) {
int length = expression.length();
//数栈
CalculatorStack numberStack = new CalculatorStack(length);
//符号栈
CalculatorStack symbolStack = new CalculatorStack(length);
//定义变量
int index = 0;
int num1 = 0;
int num2 = 0;
int symbol = 0;
int result = 0;
String keepNum = "";
char ch = ' ';
while (true) {
ch = expression.substring(index, index + 1).charAt(0);
if (symbolStack.isSymbol(ch)) {
//符号栈有值
if (!symbolStack.isEmpty()) {
//当前操作符优先级 <= 栈中的操作符
if (symbolStack.priority(ch) <= symbolStack.priority(symbolStack.peek())) {
//从数栈中pop出两个数值
num1 = numberStack.pop();
num2 = numberStack.pop();
//符号栈pop出一个符号
symbol = symbolStack.pop();
result = numberStack.calculate(num1, num2, symbol);
//运算得到结果入数栈
numberStack.push(result);
//当前操作符入符号栈
symbolStack.push(ch);
} else {
//当前操作符优先级 > 符号栈中的操作符,就直接入符号栈
symbolStack.push(ch);
}
} else {
//符号栈为空,就直接入栈
symbolStack.push(ch);
}
} else {
keepNum += ch;
if (index == length - 1) {
numberStack.push(Integer.parseInt(keepNum));
} else {
//数字下一位是符号再入栈
if (symbolStack.isSymbol(expression.substring(index + 1, index + 2).charAt(0))) {
numberStack.push(Integer.parseInt(keepNum));
keepNum = "";
}
}
}
index++;
if (index == length) {
break;
}
}
//当表达式扫描完成,就顺序的从栈中pop出相应的数值和符号,进行运算
while (true) {
if (symbolStack.isEmpty()) {
break;
}
num1 = numberStack.pop();
num2 = numberStack.pop();
symbol = symbolStack.pop();
result = numberStack.calculate(num1, num2, symbol);
//运算得到结果入数栈
numberStack.push(result);
}
//最后的数栈中剩余的一个数值就是运算结果
return numberStack.pop();
}
注: 复杂计算器亦是如此,加入其它符号的运算即可~