package chapter4;
public class Stack {
/**
* @栈
* 1,解析算术表达式
* 2,辅助遍历树的节点
* 3,辅助查找图的顶点(一种可以用来解决迷宫问题的技术)
*/
public static void main(String[] args) {
StackX t = new StackX(10);
t.push(20);
t.push(40);
t.push(60);
t.push(80);
System.out.println(t.peek());
while(!t.isEmpty()){
int value = t.pop();
System.out.print(value+" ");
}
System.out.println();
}
}
class StackX{
private int maxSize;
private int[] stackArray;
private int top;
public StackX(int s){
maxSize = s;
stackArray = new int[maxSize];
top = 0;
}
public void push(int value){
stackArray[top++] = value;
}
public int pop(){
return stackArray[--top];
}
public int peek(){
return stackArray[top-1];
}
public boolean isEmpty(){
return (top==0);
}
public boolean isFull(){
return (top==maxSize);
}
}
本文探讨了栈数据结构在解析算术表达式、辅助遍历树节点及解决迷宫问题中的应用。
1272

被折叠的 条评论
为什么被折叠?



