1. 程式人生 > >七 、棧的java實現

七 、棧的java實現

方法 blog 構造方法 int out 初始化 const .com 分享圖片

原理圖:

技術分享圖片

源代碼:


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;
}

}

七 、棧的java實現