栈
一、栈
栈(stack)又名堆栈,它是一种运算受限的线性表。
- 限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。
- 向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;
- 从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
二、栈的实现(数组)
public class ArrayStack {
/**
* 栈的大小
*/
private int maxSize;
/**
* 栈顶
*/
private int top;
/**
* 实现栈的数组
*/
private int[] array;
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
this.top = -1;
this.array = new int[maxSize];
}
/**
* 判断栈是否满
* @return
*/
public boolean isFull(){
return top == maxSize - 1;
}
/**
* 判断栈是否为空
* @return
*/
public boolean isEmpty(){
return top == -1;
}
/**
* 入栈
* @param n
*/
public void push(int n){
if (isFull()){
System.out.println("栈满");
return;
}
top++;
array[top] = n;
}
/**
* 出栈
* @return
*/
public int pop(){
if (isEmpty()){
System.out.println("栈空");
return -1;
}
int value = array[top];
top--;
return value;
}
/**
* 栈有效值
* @return
*/
public int size(){
return top+1;
}
/**
* 打印栈
*/
public void print(){
if (isEmpty()){
System.out.println("栈空");
return;
}
for (int i = 0;i <= top;i++){
System.out.println(array[i]);
}
}
}
三、栈的使用实例(简易计算器)
1、项目结构
2、运算符实体类:Operator
/**
运算符实体类(只涉及加减乘除)
*/
public class Operator {
/**
* 判断字符是否未运算符
* @param c
* @return
*/
public static boolean isOperator(char c){
return c == '+' || c == '-' || c == '*' || c == '/';
}
/**
* 运算符优先级
* @param c
* @return
*/
public static int operatorPriority(char c){
if (c == '+' || c == '-'){
return 0;
}else if (c == '*' || c == '/'){
return 1;
}else{
return -1;
}
}
/**
* 运算
* @param c
* @param x
* @param y
* @return
*/
public static int operation(char c,int x,int y){
switch (c){
case '+' :
return x+y;
case '-' :
return x-y;
case '*' :
return x*y;
case '/' :
return x/y;
}
return -1;
}
}
3、简单的计算器(只支持个位数的加减乘除运行)
import java.util.*;
/**
简单的计算器
*/
public class SimpleCalculator {
/**
* 数栈
*/
ArrayStack number = new ArrayStack(99);
/**
* 运算符栈
*/
ArrayStack operator = new ArrayStack(99);
public int run(String s){
int index = 0;
char c = ' ';
int result = 0;
while (true){
c = s.substring(index,index+1).charAt(0);
if (c == ' '){
index ++;
continue;
}
if (Operator.isOperator(c)){
if (operator.isEmpty()){
operator.push(c);
}else{
int o1 = operator.pop();
int p1 = Operator.operatorPriority((char) o1);
int p2 = Operator.operatorPriority(c);
if (p2 <= p1){
int n1 = number.pop();
int n2 = number.pop();
result = Operator.operation((char) o1,n2,n1);
number.push(result);
operator.push(c);
}else{
operator.push(o1);
operator.push(c);
}
}
}else{
number.push(Integer.parseInt(s.substring(index,index+1).toString()));
}
if (index == s.length()-1){
boolean flag = !number.isEmpty();
while (flag){
int n1 = number.pop();
int n2 = number.pop();
int c1 = operator.pop();
result = Operator.operation((char) c1,n2,n1);
if (number.isEmpty()){
break;
}
number.push(result);
}
break;
}
index++;
}
return result;
}
}
4、测试
public class Test {
public static void main(String[] args) {
SimpleCalculator simpleCalculator = new SimpleCalculator();
System.out.println(simpleCalculator.run("1+2+3+4"));
System.out.println(simpleCalculator.run("1-2-3-4"));
System.out.println(simpleCalculator.run("1*2*3*4"));
System.out.println(simpleCalculator.run("1/2/3/4"));
System.out.println(simpleCalculator.run("1+2-3*4/1"));
}
}