数据结构-用数组实现栈
原创
©著作权归作者所有:来自51CTO博客作者云原生总监的原创作品,请联系作者获取转载授权,否则将追究法律责任
package com.data.struct;
public class ArrayStack {
private int[] data;
private int top;
public ArrayStack(int size){
data=new int[size];
top=-1;
}
public void push(int d)throws Exception{
top=top+1;
if(top>=data.length){
throw new Exception("exceed");
}
data[top]=d;
}
public int pop()throws Exception{
if(top==-1){
throw new Exception("no data");
}
top=top-1;
return data[top+1];
}
public static void main(String[] args)throws Exception {
ArrayStack stack=new ArrayStack(10);
stack.push(1);
stack.push(10);
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}