arraystact.java

来自「java 计算实现代码 利用堆栈实现 java 计算实现代码」· Java 代码 · 共 57 行

JAVA
57
字号
package org.yancan;
//利用数组实现简单的堆栈
public class arrayStact 
{
  private int pointcounter;//stact bottom pointer
  int[] stact = new int[100];
  
  public arrayStact()
  {
   for(int i =0 ;i<stact.length;i++)
    stact[i] = -1;
   pointcounter = 0;
  }
  public void push(int temp)
  {
 pointcounter++;
 
 if(pointcounter<100)
 {  
  for(int i=pointcounter;i>0;i--)
   stact[i]=stact[i-1];
     stact[0] = temp;
  
 }
 else
  System.out.println("the length of expression is over the length of stact~!\n");
   }
  public int pop()
   {
   int temptop=-1;
   if(isEmpty())
   {  
    System.out.println("the stact is empty!\n");
   }
   else 
   {
    temptop = stact[0];
       for(int i =0;i<pointcounter;i++)
      stact[i]=stact[i+1];
       
    pointcounter--;
   }
   return temptop;
   }
  public int gettop()
   {
    return  stact[0];
   }
  private boolean isEmpty()
   {
    if(pointcounter==0)
     return true;
    else
     return false;
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?