java通过数组描述堆栈

来源:互联网 发布:mysql free result 编辑:程序博客网 时间:2024/04/28 08:54
public class StackArray
{
 int MaxSize=20;
 int Top=-1;
 int[] AStack=new int[MaxSize];
//-------------------------------
//入栈
//-------------------------------
public void Push(int data)
  {
    if(Top>=MaxSize)
      System.out.print("The stack is full!");
    else
      AStack[++Top]=data;

  }
//-------------------------------
//出栈
//-------------------------------
public int Pop()
  {
    if(Top<0)
      System.out.print("The stack is empty!");
    else
      return AStack[Top--];

  }
//-----------------------------
//打印
//-----------------------------
public void Print()
  {
    for(int i=0;i<MaxSize;i++)
        System.out.print("["+AStack[i]+"]");
  }
}