Queue及Deque

package collection;

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

public class DequeDemol {
	public static void main(String[] args) {
		Mystack stack = new Mystack(3);
		stack.push("a1");
		stack.push("a2");
		stack.push("a3");
		stack.push("a4");//超过栈的指定容量,所以没能加进去
		System.out.println(stack.size());
		System.out.println(stack.peek());
		System.out.println(stack.size());//获取并非移除
		
		/*System.out.println(stack.pop());
		System.out.println(stack.size());//弹栈移除
*/	}
}
class Mystack{
	private Deque container = new ArrayDeque<>();
	private int cap;
	public Mystack(int cap) {
		super();
		this.cap = cap;
	}
	//压栈
	public boolean push(T t) {
		if(container.size()+1>cap) {
			return false;
		}
		return container.offerLast(t);
	}
	//弹栈 获取最后一个进去的,后进先出
	public T pop() {
		return container.pollLast();
	}
	//获取头元素但并不移除,获取最后一个加进去的
	public T peek() {
		return container.peekLast();
	}
	public int size() {
		return container.size();
	}
	
}

单向队列(一端操作):Queue

双向队列(两端操作):Deque

你可能感兴趣的:(Queue及Deque)