Java数据结构与算法(三)——栈

一、数组模拟栈

package Stack;

import java.util.Scanner;

public class ArrayStackDemo {
	public static void main(String[] args) {
		ArrayStack stack = new ArrayStack(4);
		String key = " ";
		boolean loop = true; //控制是否退出菜单
		Scanner scanner = new Scanner(System.in);
		
		while(loop){
			System.out.println("show: 显示栈");
			System.out.println("exit: 退出栈");
			System.out.println("push: 入栈");
			System.out.println("pop: 出栈");
			System.out.println("请输入你的选择:");
			
			key = scanner.next();
			
			switch (key) {
			case "show":
				stack.list();
				break;
			case "exit":
				scanner.close();
				loop = false;
				break;
			case "push":
				System.out.println("请输入一个数:");
				int value = scanner.nextInt();
				stack.push(value);
				break;
			case "pop":
				try {
					int res = stack.pop();
					System.out.println(res);
				} catch (Exception e) {
					e.getMessage();
				}
				break;
			default:
				break;
			}
			
		}
		System.out.println("程序退出");
		
	}
}

//定义一个 ArrayStack 表示栈
class ArrayStack{
	private int maxsize; //表示栈的容量
	private int[] stack; //数组模拟栈,数据就存放在该数组中
	private int top = -1; //top表示栈顶
	
	//构造器
	public ArrayStack(int maxsize) {
		this.maxsize = maxsize;
		stack = new int[this.maxsize];
	}
	
	//判断栈满
	public boolean isFul1() {
		return top == maxsize - 1;
	}
	
	//判断栈空
	public boolean isEmpty() {
		return top == -1;
	}
	
	//入栈
	public void push(int value) {
		//先判断栈是否满
		if (isFul1()) {
			System.out.println("栈满");
			return;
		}
		top++;
		stack[top] = value;
	}
	
	//出栈
	public int pop() {
		//先判断栈是否空
		if (isEmpty()) {
			//抛出异常
			throw new RuntimeException("栈空");	
		}
		top--;
		int value = stack[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]);
		}
	}
	

}

二、栈实现综合计算器

package Stack;

public class Calculator {
	public static void main(String[] args) {
		String expression = "3+2*6-2";
		//创建两个栈,数栈,符号栈
		ArrayStack2 numStack = new ArrayStack2(10);
		ArrayStack2 operStack = new ArrayStack2(10);
		//定义需要的相关变量
		int index = 0; //用于扫描
		int num1 = 0;
		int num2 = 0;
		int oper = 0;
		int res = 0;
		char ch = ' '; //将每次扫描得到的符号保存
		String string = "";
		
		//开始循环扫描expression
		while (true) {
			//依次得到expression中的每一个字符
			ch = expression.substring(index, index+1).charAt(0); //左闭右开
			
			//判断字符是否是运算符
			if (operStack.isOper(ch)) {
				//如果是运算符,判断符号栈是否空
				if (operStack.isEmpty()) {
					//如果栈空,直接入栈
					operStack.push(ch);
				}else {
					//如果栈中有运算符,判断ch优先级是否小于等于栈顶运算符优先级
					if (operStack.priority(ch) <= operStack.priority(operStack.top()) ) {
						//如果优先级小于等于栈顶元素,从数栈中弹出两个数,从符号栈中弹出一个符号进行运算,结果保存在数栈中
						num1 = numStack.pop();
						num2 = numStack.pop();
						oper = operStack.pop();
						res = numStack.cal(num1, num2, oper);
						numStack.push(res);
						operStack.push(ch); //将ch保存在符号栈中
					}else {
						//如果栈中有运算符,ch优先级大于栈顶运算符优先级,ch直接入符号栈
						operStack.push(ch);
					}
				}
			}else {
				//先创建一个字符串,用来保存合并后的字符
				string = string + ch;
				//如果不是运算符,先判断index是否是最后一个
				if (index == expression.length()-1) {
					numStack.push(Integer.parseInt(string));
				}else {
					//先判断是否是一位数,如果不是一位数,则需要合并字符
					//判断数字后一位是否是字符,如果是字符,直接入数栈
					if (operStack.isOper(expression.substring(index+1, index+2).charAt(0))) {
						numStack.push(Integer.parseInt(string)); //將字符串转换为int数字
						string = "";
					}
				}	
			}
			
			index++; //index后移
			//判断index是否是字符串最后数字的后一个
			if (index >= expression.length()) {
				break;
			}
		}
		
		//运算结束后就依次从符号栈和数栈中取数进行运算
		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("表达式的运行结果:%d\n",numStack.pop());
		
		
		
		
	}
}

//先创建一个栈
//定义一个ArrayStack2 表示栈,需要扩展功能,判断是符号还是数字,如果是符号,判断优先级
class ArrayStack2{
	private int maxsize; //表示栈的容量
	private int[] stack; //数组模拟栈,数据就存放在该数组中
	private int top = -1; //top表示栈顶
	
	//构造器
	public ArrayStack2(int maxsize) {
		this.maxsize = maxsize;
		stack = new int[this.maxsize];
	}
	
	//判断栈满
	public boolean isFul1() {
		return top == maxsize - 1;
	}
	
	//判断栈空
	public boolean isEmpty() {
		return top == -1;
	}
	
	//入栈
	public void push(int value) {
		//先判断栈是否满
		if (isFul1()) {
			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 = num1 / num2;
			break;
		default:
			break;
		}
		return res;
	}
	
	//获取栈顶元素
	public int top() {
		return stack[top];
	}
	
	
	
}

三、栈实现后缀表达式的求值

package Stack;

import java.util.List;
import java.util.Stack;
import java.util.ArrayList;


public class PolandNotation {

	public static void main(String[] args) {
		//先定义一个逆波兰表达式
		//为了方便,逆波兰表达式的数字和符号之间用空格隔开
		String suffixException = "3 4 + 5 * 6 -";
		//思路:
		//先将"3 4 + 5 * 6 -"放到ArrayList中
		//将ArrayList传递给一个方法,遍历ArrayList,配合栈完成计算
		List<String> rpnlist = getListString(suffixException);
		System.out.println("rpnlist = " + rpnlist);
		
		int res = cal(rpnlist);
		System.out.println(res);
		
	}

	
	//将数字和运算符放入ArrayList中
	public static List<String> getListString(String suffixException) {
		//将suffixException分割,根据空格分隔,将每个数字和运算符存入一个字符串数组
		String[] splitString = suffixException.split(" ");
		List<String> list = new ArrayList<String>();
		for(String ele : splitString){
			list.add(ele);
		}
		return list;
	}
	
	//完成对逆波兰表达式的计算
	public static int cal(List<String> ls) {
		Stack<String> stack = new Stack<>();
		//遍历ls,将字符串压入栈中
		for(String ele : ls){
			if (ele.matches("\\d+")) { //匹配的是多位数
				stack.push(ele);
			}else { //如果是运算符pop出两个数,运算后入栈
				int num1 = Integer.parseInt(stack.pop());
				int num2 = Integer.parseInt(stack.pop());
				int res = 0;
				if (ele.equals("+")) {
					res = num1 + num2;
				}else if (ele.equals("-")) {
					res = num2 - num1;
				}else if (ele.equals("*")) {
					res = num2 * num1;
				}else if (ele.equals("/")) {
					res = num2 / num1;
				}else {
					throw new RuntimeException("符号错误");
				}
				stack.push(res + "");
			}
		}
		
		return Integer.parseInt(stack.pop());
		
	}

}

四、中缀表达式转后缀表达式

package Stack;

import java.util.List;
import java.util.Stack;
import java.util.ArrayList;


public class PolandNotation {

	public static void main(String[] args) {
		
		//完成一个中缀表达式转后缀表达式的功能
		//1、1+((2+3)*4)-5 转成 1 2 3 + 4 * + 5 -
		//2、因为直接对str进行操作不方便,因此,先将1+((2+3)*4)-5转成中缀表达式对应的List
		//3、将中缀表达式对应的list转成后缀表达式对应的list
		
		
		String expression = "1+((2+3)*4)-5"; //需要转换的中缀表达式
		List<String> infixExpressionList = toInfixExpressionList(expression); //将中缀表达式转换成对应的List
		System.out.println("中缀表达式对应的List:" + infixExpressionList);
		
		//将中缀表达式对应的list转成后缀表达式对应的list
		List<String> suffixExpressionList = parseSuffixExpressionList(infixExpressionList); 
		System.out.println("后缀表达式对应的List:" + suffixExpressionList);
		
		/*
		
		//先定义一个逆波兰表达式
		//为了方便,逆波兰表达式的数字和符号之间用空格隔开
		String suffixException = "3 4 + 5 * 6 -";
		//思路:
		//先将"3 4 + 5 * 6 -"放到ArrayList中
		//将ArrayList传递给一个方法,遍历ArrayList,配合栈完成计算
		List<String> rpnlist = getListString(suffixException); //将后缀表达式的数字和运算符放入ArrayList中
		System.out.println("rpnlist = " + rpnlist); 
		
		int res = cal(rpnlist); //完成对逆波兰表达式的计算
		System.out.println(res);
		
		*/
	}
	
	
	//将中缀表达式转换成对应的后缀表达式
	public static List<String> parseSuffixExpressionList(List<String> ls) {
		//创建s1栈存放运算符,因为s2栈没有弹出,为了放便,用ArrayList s2代替s2栈
		Stack<String> s1 = new Stack<String>(); //符号栈
		List<String> s2 = new ArrayList<String>(); //储存中间结果的List s2
		
		//扫描中缀表达式中的符号和数字
		for (String item : ls) {
			//按照中缀表达式转后缀表达式的步骤进行
			//先判断是否是多位数,用正则表达式方便对比
			if (item.matches("\\d+")) {
				//如果是数字,直接入s2
				s2.add(item);
			}else if (item.equals("(")) { //如果是左括号
				s1.push(item);
			}else if (item.equals(")")) { //如果是右括号
				while (!s1.peek().equals("(")) {
					s2.add(s1.pop());
				}
				s1.pop(); //弹出s1中的左括号
			}else { //如果是运算符
				//当s1栈顶运算符优先级高于item时,将s1中运算符弹出放入s2中
				while (s1.size() != 0 && priority.getPriority(s1.peek()) >= priority.getPriority(item)) {
					//s1不为空且当s1栈顶运算符优先级高于item时,将s1中运算符弹出放入s2中
					s2.add(s1.pop());
				}
				//否则,当s1栈顶运算符优先级低于item时,item直接入s1栈
				s1.push(item);
			}
		} //运行结束时,已读到表达式最右端
		
		//将s1中符号弹出存入s2中
		while (s1.size() != 0) {
			s2.add(s1.pop());
		}
		return s2; //因为是存放到List中,因此按顺序输出就是转换后的后缀表达式
	}
	
	
	
	

	//将中缀表达式转换成对应的List
	public static List<String> toInfixExpressionList(String s) {
		//定义一个List存放中缀表达式对应的内容
		List<String> list = new ArrayList<String>();
		int i = 0; //这是一个指针,用于遍历中缀表达式对应的内容
		String str; //用于对多位数的拼接
		char c; //每遍历到一个字符就放入到c中
		
		do {
			//如果c是一个非数字就需要放入到ls中
			if ((c=s.charAt(i)) < 48 || (c=s.charAt(i)) > 57) {
				list.add(c + "");
				i++;
			}else {
				//如果是一个数,需要考虑多位数的问题
				str = ""; //注意!!!把数转化成字符串,“”中不能有空格
				while (i < s.length() && (c=s.charAt(i)) >= 48 && (c=s.charAt(i)) <= 57) {
					//i小于数组长度并且c是数字
					str += c;
					i++;
				}
				list.add(str);
			}
	
			
		} while (i < s.length());
		return list;
	}
	
	

	
	
	
	
	//将数字和运算符放入ArrayList中
	public static List<String> getListString(String suffixException) {
		//将suffixException分割,根据空格分隔,将每个数字和运算符存入一个字符串数组
		String[] splitString = suffixException.split(" ");
		List<String> list = new ArrayList<String>();
		for(String ele : splitString){
			list.add(ele);
		}
		return list;
	}
	
	//完成对逆波兰表达式的计算
	public static int cal(List<String> ls) {
		Stack<String> stack = new Stack<>();
		//遍历ls,将字符串压入栈中
		for(String ele : ls){
			if (ele.matches("\\d+")) { //匹配的是多位数
				stack.push(ele);
			}else { //如果是运算符pop出两个数,运算后入栈
				int num1 = Integer.parseInt(stack.pop());
				int num2 = Integer.parseInt(stack.pop());
				int res = 0;
				if (ele.equals("+")) {
					res = num1 + num2;
				}else if (ele.equals("-")) {
					res = num2 - num1;
				}else if (ele.equals("*")) {
					res = num2 * num1;
				}else if (ele.equals("/")) {
					res = num2 / num1;
				}else {
					throw new RuntimeException("符号错误");
				}
				stack.push(res + "");
			}
		}
		
		return Integer.parseInt(stack.pop());
		
	}

}

//创建一个priority类,比较栈中运算符优先级
class priority{
	private static int add = 1;
	private static int sub = 1;
	private static int mul = 2;
	private static int div = 2;
	public static int getPriority(String oper) {
		int res = 0;
		switch (oper) {
		case "+":
			res = add;
			break;
		case "-":
			res = sub;
			break;
		case "*":
			res = mul;
			break;
		case "/":
			res = div;
			break;
		default:
			System.out.println("不存在该运算符" + oper);
			break;
		}
		return res;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值