ArrayDeque实现栈

package sort;

import java.util.ArrayDeque;
import java.util.Deque;

public class MyStack {
	//容器
	private Deque container=new ArrayDeque();
	//容量
	private int cap;
	
	public MyStack(int cap){
		super();
		this.cap=cap;
	}
	
	//压站
	public boolean push(E e){
		if(this.container.size()+1>this.cap){
			return false;
		}
		return this.container.offerLast(e);
		
	}
	
	//弹站
	public E pop(){
		return this.container.pollLast();
	}
	
	//出战
	public E peak(){
		return this.container.peekLast();
	}
	
	public int size(){
		return this.container.size();
	}
	
	public static void main(String[] args) {
		MyStack stack=new MyStack(10);
		stack.push("a");
		stack.push("b");
		System.out.println(stack.pop());
		System.out.println(stack.pop());
		
	}

}

你可能感兴趣的:(ArrayDeque实现栈)