スタック----静的スタック実証アルゴリズム
2066 ワード
/**
*
*/
package com.szy.structure.stack;
import java.util.Scanner;
public class StaticStack {
private final int SIZE=5;
private int top;
private int[] stackArray=null;
StaticStack(){
top=-1;
stackArray=new int[SIZE];
}
/**
*
* @return
*/
public boolean isFull()
{
if (top==SIZE-1) {
return true;
}
return false;
}
/**
*
* @return
*/
public boolean isEmpty()
{
if (top==-1) {
return true;
}
return false;
}
/**
*
* @param info
*/
public void push(int info){
top++;
stackArray[top]=info;
System.out.println(info+" ");
}
/**
*
*/
public void pop(){
System.out.println(stackArray[top]+" ");
top--;
}
/**
*
*/
public void display(){
if (isEmpty()) {
System.out.println(" ");
}
else {
System.out.println(" :");
for(int i=top;i>-1;i--){
System.out.print(stackArray[i]+" ");
}
System.out.println("
");
}
}
public static void main(String[] args) throws Exception {
StaticStack stack=new StaticStack();
int choice=0;
while(true)
{
System.out.println("---MENU---");
System.out.println("1.Push");
System.out.println("2.Pop");
System.out.println("3.Display");
System.out.println("4.Exit");
System.out.println("----------");
System.out.println(" :");
Scanner scanner=new Scanner(System.in);
choice=scanner.nextInt();
switch (choice) {
case 1:
{
if (stack.isFull()) {
System.out.println(" !");
break;
}
System.out.println(" :");
int input=scanner.nextInt();
stack.push(input);
}
break;
case 2:
{
if (stack.isEmpty()) {
System.out.println(" !");
break;
}
stack.pop();
}
break;
case 3:
{
stack.display();
}
break;
case 4:
return;
default:
System.err.println(" !");
break;
}
}
}
}