1.什么是棧
先進后出,元素的刪除和插入只能在同一端的一種線性表
2.棧的實現(xiàn)方式
數(shù)組和鏈表都可以,本次使用數(shù)組
3.什么是中綴表達式
3+2-1*6+10
4.代碼:
/**
* @author shengjk1
* @date 2020/2/13
*/
public class Calcaulator {
public static void main(String[] args) {
// 中綴表達式
String expression = "5-1*6+2";
//創(chuàng)建兩個棧 數(shù)棧、符號棧
ArrayStack1 numStack = new ArrayStack1(10);
ArrayStack1 operStack = new ArrayStack1(10);
//用于遍歷
int index = 0;
int num1, num2 = 0;
int oper = 0;
int res = 0;
//每次掃描得到的char
char ch = ' ';
//用來拼接多位數(shù)
String keepNum = "";
while (true) {
ch = expression.substring(index, index + 1).charAt(0);
//判斷 ch 是什么,然后做相應的處理
if (operStack.isOper(ch)) {
//判斷當前的符號棧是否為空
if (!operStack.isEmpty()) {
//如果當前的操作符的優(yōu)先級小于等于棧中符號的優(yōu)先級,就需要從數(shù)棧中 pop 兩個數(shù)
//在從符號棧中 pop 出一個符號,進行運算,將得到結果,入數(shù)棧,然后將當前的操作符入符號棧
if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
//運算的結果入數(shù)棧
numStack.push(res);
//然后將當前的操作符入符號棧
operStack.push(ch);
} else {
//如果當前的操作符大于棧中的操作符,就直接入符號棧
operStack.push(ch);
}
} else {
//如果為空,直接入符號棧
operStack.push(ch);
}
} else {
// 如果是數(shù)字則直接入數(shù)棧
// numStack.push(ch - 48);
//看 index 后一位,如果是數(shù)則繼續(xù)進行掃描,如果不是則入棧
keepNum += ch;
if (index == expression.length() - 1) {
numStack.push(Integer.parseInt(keepNum));
} else {
if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
numStack.push(Integer.parseInt(keepNum));
keepNum = "";
}
}
}
//讓 index +1,并判斷是否掃描到 exoression 最后
index++;
if (index >= expression.length()) {
break;
}
}
//當表達式掃描完畢,就順序的從數(shù)棧和符號棧中pop出相應的數(shù)和符號,并運行
while (true) {
if (operStack.isEmpty()) {
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
//運算的結果入數(shù)棧
numStack.push(res);
}
System.out.printf("表達式 %s = %d ", expression, numStack.pop());
}
}
class ArrayStack1 {
private int maxSize;
private int[] stack;
private int top = -1;
public ArrayStack1(int maxSize) {
this.maxSize = maxSize;
stack = new int[maxSize];
}
// 棧滿
public boolean isFull() {
return top == maxSize - 1;
}
//??? public boolean isEmpty() {
return top == -1;
}
//查看當前棧頂?shù)闹? public int peek() {
return stack[top];
}
//入棧
public void push(int element) {
if (isFull()) {
System.out.println("棧滿");
return;
}
top++;
stack[top] = element;
}
//出棧
public int pop() {
if (isEmpty()) {
throw new RuntimeException("stack is empty!");
}
int temp = stack[top];
// stack[top]=null; 防止內存泄露
top--;
return temp;
}
//
public void list() {
if (isEmpty()) {
System.out.println("stack is empty");
return;
}
for (int i = top; i >= 0; i--) {
System.out.printf("stack[%d]=%d\n", i, stack[i]);
}
}
//返回運算符的優(yōu)先級 假設優(yōu)先級越高返回的數(shù)字越大
public int priority(int oper) {
if (oper == '*' || oper == '/') {
return 1;
} else if (oper == '+' || oper == '-') {
return 0;
} else {
//假設目前表達式只有 + - * /
return -1;
}
}
/**
* @param val
* @return
*/
public boolean isOper(char val) {
return val == '+' || val == '-' || val == '*' || val == '/';
}
/**
* 計算
*
* @param num1
* @param num2
* @param oper
* @return
*/
public int cal(int num1, int num2, int oper) {
int res = 0;
switch (oper) {
case '+':
res = num1 + num2;
break;
case '-':
//注意順序
res = num2 - num1;
break;
case '*':
res = num1 * num2;
break;
case '/':
res = num2 / num1;
break;
default:
break;
}
return res;
}
}
5.棧的使用場景:
1.遞歸
2.方法調用
3.表達式的轉化和求值
4.二叉樹遍歷
5.圖的深度優(yōu)先遍歷
6.逆序輸出 如 單鏈表的反轉
6.面試題
如何用兩個棧達到一個隊列的效果