1.特点:栈是先进后出的。
2.数组模拟栈的分析图
实现 栈的 思路分析
1. 使用数组来模拟栈
2. 定义一个 top 来表示栈顶,初始化 为 -1
3. 入栈的操作,当有数据加入到栈时, top++; stack[top] = data;
4. 出栈的操作, int value = stack[top]; top--, return value
代码实现栈
public class ArrayStack {
//栈需求实现
private int maxLength;
private int[] stack;
private int top = -1;
public ArrayStack(int maxLength) {
this.maxLength = maxLength;
stack = new int[maxLength];
}
public boolean isFull() {
return top == maxLength - 1;
}
public boolean isEmpty() {
return top == -1;
}
public void push(int val) {
if (isFull()) {
throw new RuntimeException("对不起,已满");
}
top++;
stack[top] = val;
}
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\t", i, stack[i]);
}
}
//栈中元素存在的个数
public int length(){
return this.top+1;
}
}
判断回文(利用栈)
public class TestApp {
//判断回文
//hello olleh 不是一个回文
public static void main(String[] args) {
System.out.println(detecation("aba"));
}
public static boolean detecation(String val){
ArrayStack arrayStack = new ArrayStack(10);
//获取字符串长度
int length =val.length();
//放入栈
for(int i =0;i<length;i++){
//注意:这里将a转化成97
arrayStack.push(val.charAt(i));
}
//获取
String newVal = "";
int length1=arrayStack.length();
for(int i =0;i<length1;i++){
if(!arrayStack.isEmpty()){
char pop =(char)arrayStack.pop();
newVal=newVal+pop;
}
}
/* equals方法的源代码:
public boolean euqals(Object obj){
return(this==obj);
*/
if(val.equals(newVal)){
return true;
}
return false;
}
}
例题:计算机需求分析
public class ArrayStack {
//栈需求实现
private int maxLength;
private int[] stack;
private int top = -1;
public ArrayStack(int maxLength) {
this.maxLength = maxLength;
stack = new int[maxLength];
}
public boolean isFull() {
return top == maxLength - 1;
}
public boolean isEmpty() {
return top == -1;
}
public void push(int val) {
if (isFull()) {
throw new RuntimeException("对不起,已满");
}
top++;
stack[top] = val;
}
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\t", i, stack[i]);
}
}
//栈中元素存在的个数
public int length() {
return this.top + 1;
}
//判断是否是一个运算符 + - * /
public boolean isOper(char v) {
return v == '+' || v == '-' || v == '*' || v == '/';
}
//判断运算符优先级,数字越大的优先级越大
public int priority(int oper) {
if (oper == '*' || oper == '/') {
return 1;
} else if (oper == '+' || oper == '-') {
return 0;
} else {
return -1;
}
}
//获取栈顶数据
public int peek() {
return this.stack[top];
}
//获取栈容量
public int stackLength() {
return this.stack.length;
}
/**
* 计算两个数进行运算后的结果
* 2+3
* 3:num1,2:num2
*/
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;
}
}
public class TestStack {
public static void main(String[] args) {
String str="4+3+2*3-1";
//1、需要遍历字符串,获取每一个字符。
//2、判断当前字符是一个运算符还是数字
//3、把数字放入数字栈,把运算符放入运算符栈
//4、运算符栈:如果是一个空栈,那么运算符直接入栈,如果运算符栈中已经
//有其他运算符就需要先对比运算符优先级,新进来的运算符如果小于等于原栈中运算符,
//那么需要把原运算符弹出来,数字栈中的数字进行弹栈,进行运算,运算后的结果重新
//放入数字栈,新运算符直接入栈,如果新的运算符优先级大于原栈中的运算符,那么运算符直接入栈。
ArrayStack numStack = new ArrayStack(10);
ArrayStack symbolStack = new ArrayStack(10);
//获取字符串的长度
int temp1=0;
int temp2=0;
int symbolChar = 0;
int result = 0;
int length =str.length();
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())){
//1、去符号栈中获取栈顶的符号
//2、去数字栈中获取两个数字
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+44
values +=c;
if(i==length-1){
numStack.push(Integer.parseInt(values));
}else{
//substring包括起始,不包括终止
char data = str.substring(i+1,i+2).charAt(0);
if(symbolStack.isOper(data)){
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);
}
}