class arrStack
package DataStructures.stack;
/**
* 用数组模拟栈
* @author coffee
* @date 2021-03-30 17:00
*/
public class arrStack {
private int maxSize;
private int[] stack;
private int top = -1;
public arrStack(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
/**
* 栈空
* @return
*/
public boolean isFull(){
return top == maxSize -1;
}
/**
* 栈空
* @return
*/
public boolean isEmpty(){
return top == -1;
}
/**
* 入栈
* @param value
*/
public void push(int value){
if (isFull()){
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
/**
* 出栈
* @return
*/
public int pop(){
if (isEmpty()){
throw new RuntimeException("栈满");
}
int i = stack[top];
top--;
return i;
}
/**
* 遍历栈
*/
public void list(){
if (isEmpty()){
System.out.println("栈空");
return;
}
for (int i = top; i >= 0 ; i--) {
System.out.print(stack[i]);
}
}
/**
* 返回运算符的优先级,数字越大,优先级越高
* @param ope
* @return
*/
public static int priority(int ope){
if (ope == '*' || ope == '/'){
return 1;
}else if (ope == '+' || ope == '-'){
return 0;
}else
return -1;
}
/**
* 判断是否为运算符
* @param ope
* @return
*/
public boolean isOpe(char ope){
return ope == '+'||
ope == '-' ||
ope == '*' ||
ope == '/';
}
/**
* 计算方法
* @param n1
* @param n2
* @param ope
* @return
*/
public int cal(int n1,int n2,int ope){
int result = 0;
switch (ope){
case '+':
result = n1 + n2;
break;
case '-':
result = n2 - n1;
break;
case '*':
result = n1 * n2;
break;
case '/':
result = n2 / n1;
break;
}
return result;
}
/**
* 可以用来查看栈顶的值
* @return
*/
public int peek(){
return stack[top];
}
}