主子很萌:平
源碼截圖1
源碼截圖2
程式源碼
package Review;
public class StackX {
private long[] stackArray;//用數組來存儲資料,是以建立一個長整型數組stackArray,用數組建構一個棧
private int maxSize;//數組長度,即最大可存多少資料
private int top;//棧頂指針
public StackX(int maxSize){
this.maxSize=maxSize;
stackArray=new long[ maxSize];
top=-1;
}
public void push(int j){
stackArray[++top]=j;//相當于top++;stackArray[top]=j;
}
public long pop(){
return stackArray[top--];
}
public long peek(){
return stackArray[top];
}
public boolean isEmpty(){
return top==-1;
}
public boolean isFull(){
return top==(maxSize-1);
}
}
package Review;
import java.util.Scanner;
public class StackTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc= new Scanner(System.in);
System.out.println("請輸入數組容量");
int num=sc.nextInt();
StackX sn=new StackX(num);
sn.push(20);
sn.push(40);
sn.push(60);
sn.push(80);
while(!sn.isEmpty()){//類似for循環輸出
long b =sn.pop();
System.out.print(" "+b);
}
System.out.println();
System.out.println("sn.isEmpty()="+sn.isEmpty());
System.out.println("sn.isFull():"+sn.isFull());
}
}
參考文獻: 。。。。。。。。。。。。我随手寫的,哪有什麼參考