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就差不多了,
仅用于记录学习过程