一、使用数组模拟栈。
package ACStudy;
public class StackDemo {
public static void main(String[] args) {
Stack stack = new Stack(4);
stack.push(9);
stack.push(2);
stack.push(3);
stack.pop();
stack.show();
}
}
class Stack{
private int maxSize;
public int top =-1;
public int[] arr;
public Stack(int maxSize) {
this.maxSize = maxSize;
arr=new int[maxSize];
}
//判断是否为空
public boolean isNull(){
boolean flag=false;
if (top==-1){
flag=true;
System.out.println("栈空");
}
return flag;
}
//判断栈满
public boolean isFull(){
boolean flag =false;
if (top==maxSize-1){
flag=true;
System.out.println("栈满");
}
return flag;
}
public void push(int i){
if (isFull()){
throw new RuntimeException("栈满");
}
top++;
arr[top]=i;
}
public int pop(){
if (isNull()){
throw new RuntimeException("栈空");
}
int temp = top;
top--;
return arr[temp];
}
public void show(){
int temp =top;
for (int i = 0; i <=top; i++) {
System.out.println(arr[temp]);
temp--;
}
}
}
前缀,中缀、后缀表达式
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class PolandNotation {
public static void main(String[] args) {
//定义逆波兰表达式
String str ="3 4 + 5 * 6 - ";
String[] s = str.split(" ");
List<String> list =new ArrayList<>();
list.addAll(Arrays.asList(s));
int res = clculate(list);
System.out.println("计算的结果是"+res);
}
public static int clculate(List<String> ls){
//创建,只需要一个栈
Stack<String> stack =new Stack<>();
for (String l : ls) {
if (l.matches("\\d+")){//匹配的是多位数
//入栈
stack.push(l);
}else {
//pop出两个数,先运算,再入栈
int num2 =Integer.parseInt(stack.pop());
int num1= Integer.parseInt(stack.pop());
int res =0;
if (l.equals("+")){
res =num1+num2;
}else if (l.equals("-")){
res=num1-num2;
}else if (l.equals("*")){
res=num1*num2;
}else if (l.equals("/")){
res=num1/num2;
}else {
throw new RuntimeException("运算符异常");
}
//把res入栈
stack.push(""+res);
}
}
//最后留在stack中的数据是运算结果
return Integer.parseInt(stack.pop());
}
}
中缀转后缀:
实现计算器功能:
import java.util.*;
import java.util.Stack;
public class PolandNotation {
public static void main(String[] args) {
//定义逆波兰表达式
//String str ="3 4 + 5 * 6 - ";
//String[] s = str.split(" ");
//List<String> list =new ArrayList<>();
//list.addAll(Arrays.asList(s));
//int res = clculate(list);
//System.out.println("计算的结果是"+res);
Scanner scanner =new Scanner(System.in);
String next = scanner.next();
char[] chars = next.toCharArray();
List<String> list1 =new ArrayList<>();
for (int i = 0; i <chars.length ; i++) {
StringBuilder s1 = new StringBuilder();
boolean flag =false;
while (i<chars.length){
//判断是否为数字
if (chars[i]<48||chars[i]>57){
if (flag){
i--;
break;
}else {
s1.append(chars[i]);
break;
}
}else {
//数字考虑多位
s1.append(chars[i]);
flag=true;
i++;
}
}
list1.add(s1.toString());
}
Iterator<String> iterator = list1.iterator();
while (iterator.hasNext()) {
String next1 = iterator.next();
System.out.print(next1);
}
System.out.println();
List<String> list = parseSuffixExpressionList(list1);
Iterator<String> iterator1 = list.iterator();
while (iterator1.hasNext()) {
String next1 = iterator1.next();
System.out.println(next1);
}
System.out.println();
int clculate = clculate(list);
System.out.println(clculate);
}
//中缀转后缀
public static List<String> parseSuffixExpressionList(List<String> list){
Stack<String> stack =new Stack<>();
List<String> list1 =new ArrayList<>();
for (String s : list) {
//数字直接加到list1
if (s.matches("\\d+")){
list1.add(s);
}else if (s.equals("(")){
//左括号直接加栈
stack.push(s);
}else if (s.equals(")")){//右括号知道下一个括号出现出栈
while (!stack.peek().equals("(")){
list1.add(stack.pop());
}
stack.pop();
}else {
//比较运算符的优先级
if (stack.size()!=0) {
while (!stack.peek().equals("(")) {
//
if (getValue(s)<= getValue(stack.peek())) {
list1.add(stack.pop());
}else {
break;
}
if (stack.size()==0){
break;
}
}
}
stack.push(s);
}
}
while (stack.size()!=0){
list1.add(stack.pop());
}
return list1;
}
public static int getValue(String str){
int result=0;
switch (str) {
case "+":
result=1;
break;
case "-":
result=2;
break;
case "*":
result=3;
break;
case "/":
result=3;
}
return result;
}
public static int clculate(List<String> ls){
//创建,只需要一个栈
Stack<String> stack =new Stack<>();
for (String l : ls) {
if (l.matches("\\d+")){//匹配的是多位数
//入栈
stack.push(l);
}else {
//pop出两个数,先运算,再入栈
int num2 =Integer.parseInt(stack.pop());
int num1= Integer.parseInt(stack.pop());
int res =0;
if (l.equals("+")){
res =num1+num2;
}else if (l.equals("-")){
res=num1-num2;
}else if (l.equals("*")){
res=num1*num2;
}else if (l.equals("/")){
res=num1/num2;
}else {
throw new RuntimeException("运算符异常");
}
//把res入栈
stack.push(""+res);
}
}
//最后留在stack中的数据是运算结果
return Integer.parseInt(stack.pop());
}
}
优先级大于等于时就要pop and add