ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

栈放入具体问题

2022-01-23 22:06:17  阅读:156  来源: 互联网

标签:maxStack return int public 问题 length 具体 stack 放入


1.栈与回文

package 数据结构;

import java.util.Scanner;

/**
 * 
 *
 */

public class ArrayStack {

	private int maxStack;
	private int[] stack;
	private int top = -1;

	public ArrayStack(int maxStack) {
		this.maxStack = maxStack;
		this.stack = new int[this.maxStack];
	}

	public boolean isFull() {
		return this.top == this.maxStack - 1;

	}

	public boolean isEmpty() {
		return this.top == -1;
	}

	public void push(int value) {
		if (isFull()) {
			System.out.println("栈已满");
		}

		stack[++top] = value;
	}

	public int pop(int value) {
		if (isEmpty()) {
			System.out.println("空栈,没有数据");
		}
		value = stack[top--];

		return value;
	}

	public void list() {
		if (isEmpty()) {
			System.out.println("空栈,没有数据");

		}
		for (int i = 0; i < stack.length; i++) {
			System.out.println(stack[i]);

		}
	}

	public int length() {
		return stack.length;
	}

	public static boolean detection(String words) {
		ArrayStack arrayStack = new ArrayStack(10);
		int length = words.length();
		for (int i = 0; i < length; i++) {
			arrayStack.push(words.charAt(i));
		}
		String newValue = "";
		int length1 = arrayStack.length();
		for (int i = 0; i < length1; i++) {
			if (!arrayStack.isEmpty()) {
				char value = (char) arrayStack.pop(i);
				newValue = newValue + value;
			}
		}
		if (words.equals(newValue)) {
			return true;
		}
		return false;
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {
			String n = in.next();
			System.out.println(detection(n));

		}
	}
}

先入先出和回文差不多。

2.符号栈数字栈

package 数据结构;

import java.util.Scanner;

/**
 * 
 *
 */
public class Teststack {
	/**
	 * 栈的大小
	 */
	private int maxStack;

	/**
	 * 数组用来模拟栈
	 */
	private int[] stack;

	/**
	 * 表示栈顶,默认值为-1
	 */
	private int top = -1;

	/**
	 *
	 * @param maxStack 初始化栈的大小
	 */
	public Teststack(int maxStack) {
		this.maxStack = maxStack;
		this.stack = new int[this.maxStack];
	}

	public boolean isFull() {
		return this.top == maxStack - 1;
	}

	public boolean isEmpty() {
		return this.top == -1;
	}

	public void push(int value) {
		if (isFull()) {
			throw new RuntimeException("栈已满...");
		}

		stack[++top] = value;
	}

	public int pop() {
		if (isEmpty()) {
			throw new RuntimeException("空栈,没有数据");
		}
		int value = stack[top];
		top--;
		return value;
	}

	public void list() {
		if (isEmpty()) {
			throw new RuntimeException("空栈,没有数据");
		}
		for (int i = 0; i < stack.length; i++) {
			System.out.printf("stack[%d]=%d\n", i, stack[i]);
		}
	}

	public int length() {
		return stack.length;
	}

	public int peek() {
		return stack[top];
	}

	public int priority(int oper) {
		if (oper == '*' || oper == '/') {
			return 1;
		} else if (oper == '+' || oper == '-') {
			return 0;
		} else {
			return -1;
		}

	}

	public boolean isOper(char v) {
		return v == '+' || v == '-' || v == '*' || v == '/';
	}

	public int calculate(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 static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		String str = "4+5*9-4/2";

		Teststack numStack = new Teststack(10);
		Teststack symbolStack = new Teststack(10);
		int length = str.length();
		int temp1 = 0;
		int temp2 = 0;
		int symbolChar = 0;
		int result = 0;
		String values = "";
		for (int i = 0; i < length; i++) {
			// 获取每一个字符
			char c = str.charAt(i);
			// 判断是否是一个字符
			if (symbolStack.isOper(c)) {
				// 如果字符栈中不是空栈
				if (!symbolStack.isEmpty()) {
					// 如果当前字符优先级小于等于字符栈中存在的
					if (symbolStack.priority(c) <= symbolStack.priority(symbolStack.peek())) {
						/**
						 * 即从数字栈中pop出来两个数字,再从符号栈中pop出来一个符号进行计算,然后把结果 然后把结果再次存入栈中
						 */
						temp1 = numStack.pop();
						temp2 = numStack.pop();
						symbolChar = symbolStack.pop();
						result = numStack.calculate(temp1, temp2, symbolChar);
						// 把计算结果再次放入栈中
						numStack.push(result);
						// 把当前符号放入栈中
						symbolStack.push(c);
					} else {
						// 如果当前符号优先级大于符号栈中的符号,直接放入符号栈中
						symbolStack.push(c);
					}
				} else {
					// 如果符号栈中是空,则也直接如符号栈
					symbolStack.push(c);
				}
			} else {
				// 如果扫描的是数字。数字很能存在多位,比如33,22,100,如何能保证这多位数字
				// 判断当前字符后一位是否是一个字符,如果是字符表示当前数字结束直接存入数字栈,如果不是字符,表示
				// 这是一个多位数字
				values += c;
				if (i == length - 1) {// 表示是最后一个数字,可以直接存放在数字栈中
					numStack.push(Integer.parseInt(values));
				} else {
					char data = str.substring(i + 1, i + 2).charAt(0);
					if (symbolStack.isOper(data)) {// 如果是符号,则把数字存入数字栈中,并清空values。
						numStack.push(Integer.parseInt(values));
						values = "";
					}
				}

			}
		}

		while (true) {
			if (symbolStack.isEmpty()) {
				break;
			}
			temp1 = numStack.pop();
			temp2 = numStack.pop();
			symbolChar = symbolStack.pop();
			result = numStack.calculate(temp1, temp2, symbolChar);
			numStack.push(result);
		}
		int res = numStack.pop();
		System.out.println("结果是:" + res);

	}

}

这段直接copy动力节点了,理解栈就行用这个做四则运算不必。作为基础玩家仔细思考那一大段if就差不多了,

仅用于记录学习过程

标签:maxStack,return,int,public,问题,length,具体,stack,放入
来源: https://blog.csdn.net/qq_62690536/article/details/122656677

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有