/**
* 栈
* Create by Administrator
* 2018/6/11 0011
* 上午 10:20
**/
public class StackX {
private int maxSixe;
private long[] stackArray;
private int top;
public StackX(int s) {
this.maxSixe = s;
this.stackArray = new long[maxSixe];
this.top = -1;
}
/**
* 入栈
* @param j
*/
public void push(long j){
if(isFull()){
System.out.println("栈已满");
}else{
this.stackArray[++top] = j;
}
}
/**
* 出栈
*/
public long pop(){
return stackArray[top--];
}
/**
* 查看
* @return
*/
public void peek(){
System.out.println(stackArray[top]);
}
/**
* 判断栈是否为空
* @return
*/
public boolean isEmpty(){
return (top == -1);
}
/**
* 判断栈是否满了
* @return
*/
public boolean isFull(){
return (top == maxSixe-1);
}
public static void main(String[] args) {
StackX stackX = new StackX(5);
stackX.push(20);
stackX.push(60);
stackX.push(40);
stackX.push(80);
stackX.push(90);
stackX.push(95);
// while (!stackX.isEmpty()){
// long value = stackX.pop();//出栈
// System.out.print("出栈:"+value);
// System.out.print(" ");
// }
System.out.println("");
stackX.peek();
}
}
java学习之—栈
最新推荐文章于 2024-01-21 19:33:37 发布
本文介绍了一个简单的栈数据结构实现,包括创建固定大小的栈、元素入栈、出栈及检查栈是否为空或已满等基本功能。通过具体的Java代码示例展示了如何进行栈的操作。
332

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



