package 使用数组模拟栈;
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack as=new ArrayStack();
as.push(1);
as.push(1);
as.push(1);
as.push(1);
as.show();
}
}
class ArrayStack{
int top=-1;
int maxsize=10;
int[] stack=new int[maxsize];
public void isFull(){
if(top==maxsize-1){
System.out.println("栈满");
return;
}
}
public void isEmpty(){
if(top==-1){
System.out.println("栈空");
return;
}
}
public void push(int value){
isFull();
top++;
stack[top]=value;
}
public int pop(){
isEmpty();
int val=stack[top];
top--;
return val;
}
public void show(){
isFull();
while(top!=-1){
System.out.println(stack[top]);
top--;
}
}
}
04栈-使用数组模拟栈
最新推荐文章于 2023-03-08 17:06:51 发布