一、栈的抽象数据类型
栈是一种用于存储数据的简单数据结构,有点类似链表或者顺序表(统称线性表),栈与线性表的最大区别是数据的存取的操作,我们可以这样认为栈(Stack)是一种特殊的线性表,其插入和删除操作只允许在线性表的一端进行,一般而言,把允许操作的一端称为栈顶(Top),不可操作的一端称为栈底(Bottom),同时把插入元素的操作称为入栈(Push),删除元素的操作称为出栈(Pop)。若栈中没有任何元素,则称为空栈,栈的结构如下图:
二、栈的代码实现
public class ArrayStackDemo {
public static void main(String[] args) {
// ArrayStack arrayStack = new ArrayStack(4);
// arrayStack.push(1);
// arrayStack.push(2);
// arrayStack.push(3);
// arrayStack.push(4);
// arrayStack.list();
// System.out.println("-------------------");
// arrayStack.pop();
// arrayStack.list();
String expression = "3+3*6-2";
//创建两个栈,数栈,一个符号栈
ArrayStack numStack = new ArrayStack(10);
ArrayStack operStack = new ArrayStack(10);
//定义需要的变量
int index = 0;//用于扫描
int num1 = 0;
int num2 = 0;
int oper = 0;
int res = 0;
char ch = ' ';//将每次扫描得到的char保存到ch
//开始while循环的扫描expression
while (true) {
//一次得到expression的每一个字符
ch = expression.substring(index, index + 1).charAt(0);
//判断ch是什么,然后做相应的处理
if (operStack.isOper(ch)) {//如果是运算法
//判断当前的符号栈是否为空
if (!operStack.isEmpty()) {
//如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小雨或者等于栈中的操作符,就虚啊哟啊从数栈中POP出两个数,
//在从符号栈中pop出一个符号栈,进行运算,将得到的结果,入数栈,然后将当前的操作符入符号栈
if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
//将运算的结果如数栈
numStack.push(res);
//然后将当前的操作符入符号栈
operStack.push(ch);
} else {
//如果当前的操作符的优先级大于栈中的操作符,就直接入符号栈
operStack.push(ch);
}
} else {
//如果为空直接入栈
operStack.push(ch);
}
} else {
//如果是数,则直接入数栈
numStack.push(ch - 48);
}
index++;
if (index >= expression.length()){
break;
}
}
//当表达式扫描完毕,就顺序的从数栈和符号栈中pop出相应的结果
while(true){
//如果符号栈为空,则计算到最后的结果,数栈中只有一个数字【结果】
if (operStack.isEmpty()){
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
numStack.push(res);
}
System.out.printf("表达式%s = %d",expression,numStack.pop());
}
//构造一个ArrayStack表示栈
static class ArrayStack {
private int maxSize; //栈的大小
private int[] stack; // 数组,数组模拟栈,数据就放在该数组
private int top = -1; //top表示栈顶,初始化为-1;
//构造器
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
//栈满
public boolean isFull() {
return top == maxSize - 1;
}
//栈空
public boolean isEmpty() {
return top == -1;
}
//入栈
public void push(int value) {
//先判断栈是否满
if (isFull()) {
System.out.println("沾满");
return;
}
top++;
stack[top] = value;
}
//出栈
public int pop() {
//先判断栈是否为空
if (isEmpty()) {
throw new RuntimeException("栈为空");
}
int value = stack[top];
top--;
return value;
}
//显示栈的情况[遍历栈]
public void list() {
if (isEmpty()) {
System.out.println("没有数据");
return;
}
for (int i = top; i >= 0; i--) {
System.out.printf("stack[%d]=%d\n", i, stack[i]);
}
}
//返回运算符的优先级,优先级是由程序员来确定的,优先级使用数字来表示
//数字越大,则优先级越高
public int priority(int oper) {
if (oper == '*' || oper == '/') {
return 1;
} else if (oper == '+' || oper == '-') {
return 0;
} else {
return -1;
}
}
//判断是不是一个运算符
public boolean isOper(char val) {
return val == '+' || val == '-' || val == '*' || val == '/';
}
//计算方法
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;
}
public int peek() {
return stack[top];
}
}
}