原理图:
源代码:
public class Mystack {
private int[] array; //数组实现栈
public int top = -1; //栈顶指针
public Mystack() {
// TODO Auto-generated constructor stub
array = new int[5]; //初始化
}
public Mystack(int maxsize) //重载构造方法
{
array = new int[maxsize];
}
public void push(int value) //添加数据
{
++top;
array[top] = value;
}
public void pop() //移除数据
{
System.out.print(array[top]+" ");
top--;
}
public void displayTop() //查看栈顶数据
{
System.out.println(array[top]);
}
public boolean IsEmpty() //判断是否为空
{
return top ==-1;
}
public boolean IsFull() //判断是否为满
{
return top == array.length-1;
}
}