import java.util.Deque;
/**
* 使用队列实现自定义堆栈
* 1、弹
* 2、压
* 3、获取头
* @author Administrator
*
* @param <E>
*/
public class MyStack<E> {
//容器
private Deque<E> container =new ArrayDeque<E>();
//容量
private int cap;
public MyStack(int cap) {
super();
this.cap = cap;
}
//压栈
public boolean push(E e){
if(container.size()+1>cap){
return false;
}
return container.offerLast(e);
}
//弹栈
public E pop(){
return container.pollLast();
}
//获取
public E peek(){
return container.peekLast();
}
public int size(){
return this.container.size();
}
}
//测试自定义堆栈
public class Demo02 {
/**
* @param args
*/
public static void main(String[] args) {
MyStack<String> backHistory =new MyStack<String>(3);
backHistory.push("www.baidu.com");
backHistory.push("www.google.com");
backHistory.push("www.sina.com");
backHistory.push("www.bjsxt.cn");
System.out.println("大小:"+backHistory.size());
//遍历
String item=null;
while(null!=(item=backHistory.pop())){
System.out.println(item);
}
}
}