MyStack

复习栈,用数组实现的栈及测试:

package com.test.data_struct;

public class MyStack {

	private int maxSize;
	private int[] stackArray;
	private int top;
	
	public MyStack(int max){
		maxSize=max;
		stackArray=new int[maxSize];
		top=-1;
	}
	
	public int pop(){
		return stackArray[top--];
	}
	
	public void push(int n){
		stackArray[++top]=n;
	}
	
	public int peek(){
		return stackArray[top];
	}
	
	public boolean isEmpty(){
		return top==-1;
	}
	
	public boolean isFull(){
		return top==maxSize-1;
	}
	

}

 

package com.test.data_struct;

import com.test.data_struct.*;

public class MyStackTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyStack ms1=new MyStack(3);
		
		int n=1;
		
		while(!ms1.isFull())
			ms1.push(n++);
		
		System.out.println(ms1.peek());
		
		while(!ms1.isEmpty())
			System.out.println(ms1.pop());

	}
}

 

你可能感兴趣的:(stack)