import java.util.Scanner;
class Main {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(4);
Scanner sc = new Scanner(System.in);
String key = "";
boolean loop = true;
while(loop){
System.out.println("show");
System.out.println("exit");
System.out.println("push");
System.out.println("pop");
System.out.print("Enter your choose:");
key = sc.nextLine();
switch(key){
case "show":
stack.list();
break;
case "exit":
sc.close();
loop = false;
break;
case "push":
System.out.println("please enter add number element:");
int value = sc.nextInt();
stack.push(value);
break;
case "pop":
try{
int res = stack.pop();
System.out.println("pop value is:" + res);
}catch(Exception e){
throw new RuntimeException(e);
}
break;
default:
System.out.println("please reenter the String");
break;
}
}
System.out.println("System is exited");
}
}
class ArrayStack{
private int maxSize;
private int[] stack;
private int top = -1;
public ArrayStack(int maxSize){
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
public boolean isFull(){
return top == (maxSize - 1);
}
public boolean isEmpty(){
return top == -1;
}
public void push(int value){
if(isFull()){
System.out.println("栈满!");
return;
}
top++;
stack[top] = value;
}
public int pop(){
if(isEmpty()){
throw new RuntimeException("栈空!");
}
int value = stack[top];
top--;
return value;
}
public void list(){
if(isEmpty()){
System.out.println("栈空!");
}
for(int i = top;i >= 0;i--){
System.out.print("stack[" + i + "]:" + stack[i] + "\t");
}
System.out.println();
}
}