import java.lang.*; public class StackX{ //----- 変数の宣言 ----- private int maxSize; private char[] stackArray; private int top; //----- コンストラクタ ----- public StackX(int s){ this.maxSize = s; this.stackArray = new char[this.maxSize]; this.top = -1; } //----- スタックのトップに項目を入れる ----- public void push(char ch){ this.stackArray[++this.top] = ch; } //----- スタックのトップから項目を取る ----- public char pop(){ return this.stackArray[this.top--]; } //----- スタックのトップを見る ----- public char peek(){ return this.stackArray[this.top]; } //----- スタックが空ならtrue ----- public boolean isEmpty(){ return this.top == -1; } //----- サイズを返す ----- public int size(){ return this.top + 1; } //----- インデックスnのところの項目を返す ----- public char peekN(int n){ return this.stackArray[n]; } //----- スタックの内容を表示する ----- public void displayStack(String s){ System.out.print(s); System.out.print("Stack (bottom-->top): "); for(int i = 0; i < this.size(); i++){ System.out.print(this.peekN(i) + " "); } System.out.println(); } }