データ構造とアルゴリズム-006スタック
スタック、すべて知っています.後進する.コードを直接見て、以下の機能を実現します.
スタックの作成
圧入値
ポップアップ値
スタックの最上位値の表示
値のセットを押し込む
出力結果
スタックの作成
圧入値
ポップアップ値
スタックの最上位値の表示
値のセットを押し込む
public class TheStack {
private String [] stackArray;
private int stackSize;
private int topOfStack = -1;
TheStack(int size){
stackSize = size;
stackArray = new String[size];
Arrays.fill(stackArray, "-1");
}
public void printStack(){
StringBuffer sb = new StringBuffer("-");
for (int i = 0; i<stackSize; i++){
sb.append("-----");
}
String septalLine= sb.toString();
System.out.println(septalLine);
for (int i = 0; i<stackSize; i++){
System.out.print("| " + i + " ");
}
System.out.println("|");
System.out.println(septalLine);
for (int i = 0; i<stackSize; i++){
if(stackArray[i].equals("-1"))
System.out.print("| ");
else
System.out.print("| " + stackArray[i] + " ");
}
System.out.println("|");
System.out.println(septalLine);
}
public void push(String input){
if(topOfStack+1<stackSize){
topOfStack++;
stackArray[topOfStack] = input;
} else System.out.println("The stack is full");
}
public String pop(){
if (topOfStack>=0){
stackArray[topOfStack]="-1";
return stackArray[topOfStack--];
} else {
System.out.println("Stack is Empty");
return "-1";
}
}
public String peek(){
if(topOfStack >=0){
return stackArray[topOfStack];
}else {
System.out.println("Stack is Empty");
return "-1";
}
}
public void pushMany(String multipleValue){
String [] manyValues = multipleValue.split(" ");
for(int i =0; i<manyValues.length; i++){
push(manyValues[i]);
}
}
public static void main(String[] args) {
System.out.println("Create a Stack");
TheStack stack = new TheStack(10);
stack.printStack();
System.out.println();
System.out.println("Push first value 10");
stack.push("10");
stack.printStack();
System.out.println();
System.out.println("Push Second value 11");
stack.push("11");
stack.printStack();
System.out.println();
System.out.println("Pop top value in the stack");
System.out.println("The value popped up is: " + stack.pop());
stack.printStack();
System.out.println();
System.out.println("Peek top value in the stack");
System.out.println("The value is " + stack.peek());
stack.printStack();
System.out.println();
System.out.println("Push a couple of values in the stack");
stack.pushMany("12 14 54 56 43");
stack.printStack();
System.out.println();
}
}
出力結果
Create a Stack
---------------------------------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---------------------------------------------------
| | | | | | | | | | |
---------------------------------------------------
Push first value 10
---------------------------------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---------------------------------------------------
| 10 | | | | | | | | | |
---------------------------------------------------
Push Second value 11
---------------------------------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---------------------------------------------------
| 10 | 11 | | | | | | | | |
---------------------------------------------------
Pop top value in the stack
The value popped up is: -1
---------------------------------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---------------------------------------------------
| 10 | | | | | | | | | |
---------------------------------------------------
Peek top value in the stack
The value is 10
---------------------------------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---------------------------------------------------
| 10 | | | | | | | | | |
---------------------------------------------------
Push a couple of values in the stack
---------------------------------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---------------------------------------------------
| 10 | 12 | 14 | 54 | 56 | 43 | | | | |
---------------------------------------------------