データ構造03.1->Stockスタックのシミュレーション(配列)


package aa.bb.cc.demo;

import java.util.Stack;

/**
 * Stack   (  )
 * @author   
 * @note         ,          ,        ,     top       stack
 */
public class MyStack {
	//Stack       (     list vector ,          ????)
	private long[] arr;
	private int top;	//Stack 
	
	//      
	public MyStack(){
		arr = new long[10];
		top = -1;
		
	}
	
	//        
	public MyStack(int maxSize){
		arr = new long[maxSize];
		top = -1;
	}
	
	//    ???
	public void push(int value){
		/**
		 *     
		 * arr[top=top+1] = value;
		 */
		arr[++top] = value;	
	}
	
	//      ???
	public long pop(){
		/**
		 *        ??????
		 */
		return arr[top--];
	}
	
	//    ???
	public long peek(){
		return arr[top];
	}
	
	//      
	public boolean isEmpty(){
		return top == -1;
	}
	
	//      
	public boolean isFull(){
		return top == arr.length -1;
	}
	
	
	/**
	 * main
	 * @param args
	 */
	public static void main(String[] args) {
		MyStack ms = new MyStack(4);
		ms.push(23);
		ms.push(1);
		ms.push(90);
		ms.push(12);
		System.out.println(ms.isEmpty());
		System.out.println(ms.isFull());
		
		System.out.println(ms.peek());
		System.out.println(ms.peek());
		System.out.println();
		
		//    ,    
		while(!ms.isEmpty()){
			System.out.println(ms.pop());
		}
		
		System.out.println(ms.isEmpty());
		System.out.println(ms.isFull());
		
		System.out.println("--------------     stack JDK  Stack  ----------------");
		
		//JDK  Stack
		Stack stack = new Stack();
		stack.push(23);
		stack.push(1);
		stack.push(90);
		stack.push(12);
		System.out.println(stack.isEmpty());
		
		System.out.println(stack.peek());
		System.out.println(stack.peek());
		System.out.println();
		
		//    ,    
		while(!stack.isEmpty()){
			System.out.println(stack.pop());
		}
		
		System.out.println(stack.isEmpty());

	}

}