栈
栈的一个实际需求
- 请输入一个表达式
- 计算式:[7*22-5+1-53-3]点击计算【如下图】

栈的介绍
- 栈的英文为stack(stack)。
- 栈是一个先入后出(FILO-First In Last Out)的有序列表。
- 栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。允许插入和删除一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)
- 根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶。而删除元素刚好相反,最后放入的元素最先删除,最先放入的元素最后删除。
- 图解说明出栈(pop)和入栈(push)的概念

- 栈的应用场景
- 子程序的调用:在跳往子程序前,会先将下个指令的地址存到堆栈中,直到子程序执行完后将地址取出,以回到原来的程序中。
- 处理递归调用:和子程序的调用类似,只是除了存储下一个指令的地址外,也将参数、区域变量等数据存入堆栈中。
- 表示的转换[中缀表达式转后缀表达式]与求值(实际解决)
- 二叉树的遍历。
- 图形的深度优先(dept-first)搜索法。
栈的快速入门
- 用数组模拟栈的使用,由于栈是一种有序列表,当然可以使用数组的结构来存储栈的数据内容。

实现思路分析,并画出示意图
import java.util.Scanner;
public class ArrayStackDemo {public static void main(String[] args) {ArrayStack arrayStack = new ArrayStack(3);Scanner scanner = new Scanner(System.in);String key;mywhile:while(true) {System.out.println("push:将一个元素压入栈中");System.out.println("pop:从栈中弹出一个元素");System.out.println("show:显示栈中所有元素");System.out.println("peek:查看栈顶元素");System.out.println("exit:退出系统");System.out.print("请选择你要执行的惭怍:");key = scanner.next();switch(key) {case "push":System.out.println("请选择你要压入栈中的元素");int value = scanner.nextInt();arrayStack.push(value);break;case "pop":try{System.out.println("弹出的元素是:" + arrayStack.pop());}catch(Exception e) {System.out.println(e.getMessage());}break;case "peek":try{System.out.println("栈顶的元素是:" + arrayStack.peek());}catch(Exception e) {System.out.println(e.getMessage());}break;case "show":arrayStack.display();break;case "exit":scanner.close();break mywhile;default:break;}}System.out.println("已退出系统,欢迎下次使用");}
}
class ArrayStack {int top;int[] arrStack;int maxSize;public ArrayStack(int maxSize) {this.maxSize = maxSize;arrStack = new int[maxSize];top = -1;}public void push(int value) {if(isFull()) {System.out.println("栈已满,不能压入");return;}arrStack[++top] = value;}public int pop() {if(isEmpty()) {throw new RuntimeException("栈为空");}return arrStack[top--];}public int peek() {if(isEmpty()) {throw new RuntimeException("栈为空");}return arrStack[top];}public void display() {if(isEmpty()) {System.out.println("栈空,没有数据");return;}for(int i = top;i >= 0;i--) {System.out.printf("stack[%d]=%d\n",i,arrStack[i]);}}public boolean isFull() {return top == maxSize - 1;}public boolean isEmpty() {return top == -1;}
}
栈实现综合计算器(中缀表达式)
- 使用栈来实现综合计算器

- 思路分析

- 代码实现
public class InffixExpressionValue {public static void main(String[] args) {String expression = "100-2*3*7-5";ArrayStack2 numStack = new ArrayStack2(10);ArrayStack2 operStack = new ArrayStack2(10);String keepNum = ""; int num1 = 0;int num2 = 0;int oper = 0;int res = 0;for(int i = 0; i < expression.length(); i++){char ch = expression.substring(i, i + 1).charAt(0);if(operStack.isOper((ch))) {if(operStack.isEmpty()) {operStack.push(ch);continue;}while(!operStack.isEmpty() && operStack.priority(ch) <= operStack.priority(operStack.peek())) {num1 = numStack.pop();num2 = numStack.pop();oper = operStack.pop();res = numStack.calculate(num1,num2,oper);numStack.push(res);}operStack.push(ch);}else {
keepNum = keepNum + ch;while((i + 1 < expression.length())) {char ch1 = expression.substring(i + 1, i + 2).charAt(0);if(operStack.isOper(ch1)) {break;}keepNum = keepNum + ch1;i++;}numStack.push(Integer.parseInt(keepNum));keepNum = "";}}while(!operStack.isEmpty()) {num1 = numStack.pop();num2 = numStack.pop();oper = operStack.pop();res = operStack.calculate(num1,num2,oper);numStack.push(res);}System.out.printf("表达式%s的值是%d",expression,numStack.pop());}}class ArrayStack2 {int top;int[] arrStack;int maxSize;public ArrayStack2(int maxSize) {this.maxSize = maxSize;arrStack = new int[maxSize];top = -1;}public void push(int value) {if(isFull()) {System.out.println("栈已满,不能压入");return;}arrStack[++top] = value;}public int pop() {if(isEmpty()) {throw new RuntimeException("栈为空");}return arrStack[top--];}public int peek() {if(isEmpty()) {throw new RuntimeException("栈为空");}return arrStack[top];}public void display() {if(isEmpty()) {System.out.println("栈空,没有数据");return;}for(int i = top;i >= 0;i--) {System.out.printf("stack[%d]=%d\n",i,arrStack[i]);}}public boolean isFull() {return top == maxSize - 1;}public boolean isEmpty() {return top == -1;}public static 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;}public static boolean isOper(int ch) {return ch == '+' || ch == '-' || ch == '*' || ch == '/';}public static int priority(int ch) {int priority = -1;switch(ch) {case '*': case '/':priority = 2;break;case '+': case '-':priority = 1;break;}return priority;}
}