目录
一:栈的思想
后进先出
二:栈的操作
压栈
弹栈
三:回文数
3.1 栈的定义
public class stackproject {
//栈的大小
private int max;
//数组模拟静态栈
private int[] array;
//栈顶元素
private int top = -1;
//栈的大小初始化
public stackproject(int maxstack) {
this.max = maxstack;
array = new int[max];
}
//压栈
public void push(int val) {
//
if(isFull()) {
throw new RuntimeException("栈已经满");
}
top++;
array[top] = val;
}
//弹栈
public int pop() {
if(ifEmpty()) {
throw new RuntimeException("栈已经空");
}
int value = array[top];
top--;
return value;
}
//是否是空栈
public boolean ifEmpty() {
return this.top == -1;
}
//是否是满栈
public boolean isFull() {
return this.top == max -1;
}
//查看栈中所有元素
public void list() {
if(ifEmpty()) {
throw new RuntimeException("栈已经空");
}
for(int i = 0; i < top; i++) {
System.out.print(array[i] + " ");
}
}
//获取栈的长度
public int stacklength() {
return top+1;
}
}
3.2 回文数的判断
public class TestApp {
public static void main(String[] args) {
System.out.print(decation("aba"));
}
public static boolean decation(String str) {
staticstack stack = new staticstack(10);
for(int i = 0; i < str.length(); i++) {
stack.push(str.charAt(i));
}
String newval = "";
int newlength = stack.length();
for(int i = 0; i < newlength; i++) {
char c = (char)stack.pop();
newval += c;
}
if(newval.equals(str)) {
return true;
}
return false;
}
}
四:计算机
4.1 栈的定义(数字栈、符号栈)
一串表达式中分为数字和字符分别压入数字栈和字符栈,然后通过判断字符的优先级来操作表达式(两个栈)的运算
4.1.1 判断是否是一个运算符
//判断是否是字符
public boolean isOper(char c) {
return c == '+' || c == '-' || c == '*' || c== '/';
}
4.1.2 判断运算符的优先级
public int priority(char c) {
if(c == '*' || c == '/')
return 1;
else if(c == '+' || c == '-')
return 0;
else
return -1;
}
4.2 计算机代码展示
4.2.1 栈的定义
package javastack;
public class stackproject {
//栈的大小
private int max;
//数组模拟静态栈
private int[] array;
//栈顶元素
private int top = -1;
//栈的大小初始化
public stackproject(int maxstack) {
this.max = maxstack;
array = new int[max];
}
//压栈
public void push(int val) {
//
if(isFull()) {
throw new RuntimeException("栈已经满");
}
top++;
array[top] = val;
}
//弹栈
public int pop() {
if(ifEmpty()) {
throw new RuntimeException("栈已经空");
}
int value = array[top];
top--;
return value;
}
//是否是空栈
public boolean ifEmpty() {
return this.top == -1;
}
//是否是满栈
public boolean isFull() {
return this.top == max -1;
}
//查看栈中所有元素
public void list() {
if(ifEmpty()) {
throw new RuntimeException("栈已经空");
}
for(int i = 0; i < top; i++) {
System.out.print(array[i] + " ");
}
}
//获取栈的长度
public int stacklength() {
return top+1;
}
//判断是否是字符
public boolean isOper(char c) {
return c == '+' || c == '-' || c == '*' || c== '/';
}
//判断栈的容量的大小
public int stacksize() {
return this.array.length;
}
//获取栈顶元素
public int peek() {
return this.array[top];
}
//判断运算符的优先级
public int priority(char c) {
if(c == '*' || c == '/')
return 1;
else if(c == '+' || c == '-')
return 0;
else
return -1;
}
//计算两个数的结果
public int calculate(int num1, int num2, int oper) {
int result = 0;
switch(oper) {
case '+':
result = num1 + num2;
break;
case '-':
result = num2 - num1;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num2 / num1;
break;
default:
break;
}
return result;
}
}
4.2.2 计算机的运算
package javastack;
public class TestApp {
public static void main(String[] args) {
String str = "4+3*2-1";
stackproject numstack = new stackproject(10);
stackproject symbolstack = new stackproject(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.ifEmpty()) {
if(symbolstack.priority(c) <= symbolstack.priority((char)symbolstack.peek())) {
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 {
//多位数字
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)) {
numstack.push(Integer.parseInt(values));
}
}
values = "";
}
}
while(true) {
if(symbolstack.ifEmpty()) {
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.print(res);
}
}