@param
*/
public class MyHeadLastLinkedDeque extends MyQueueAbstract implements MyDequeDao{
@SuppressWarnings(“rawtypes”)
private MyHeadLastList headLastListDueue;
private int size;
private int modCount;
public MyHeadLastLinkedDeque(){
headLastListDueue = SingletonUtils.getMyHeadLastListSingleton();
}
/***
@functionName:size
@description:队列的大小
@author yzh
@date 2019-04-13
@return int
*/
public int size(){
return size;
}
/queue
/***
@functionName:add
@description:实现元素的添加。
@param e
@author yzh
@date 2019-04-13
@return boolean
/
public boolean add(E e) {
this.addLast(e);
return true;
}
/**
@functionName:offer
@description:用于实现元素的添加。入队成功返回true,否则返回false,即如果队列已满直接返回false,队列未满则直接插入并返回true。
@param e
@author yzh
@date 2019-04-13
@return boolean
*/
public boolean offer(E e) {
return this.offerLast(e);
}
/***
@functionName:remove
@description:方法直接删除队头的元素。
@author yzh
@date 2019-04-13
@return E
*/
public E remove() {
return this.removeLast();
}
/***
@functionName:poll
@description:方法取出并删除队头的元素,当队列为空,返回null。
@author yzh
@date 2019-04-13
@return E
*/
public E poll() {
return this.pollLast();
}
/***
@functionName:peek
@description:方法直接取出队头的元素,并不删除。
@author yzh
@date 2019-04-13
@return E
*/
public E peek() {
return this.getFirst();
}
/***
@functionName:isEmpty
@description:判断队列是否为空
@author yzh
@date 2019-04-13
@return boolean
*/
public boolean isEmpty(){
return size == 0;
}
///deque/
/***
@functionName:addFirst
@description:从对头添加元素
@author yzh
@date 2019-04-20
@return
*/
@SuppressWarnings(“unchecked”)
@Override
public void addFirst(E e) {
if (e == null){
throw new NullPointerException();
}
headLastListDueue.addByHead(e);
modCount++;
size++;
}
/***
@functionName:addLast
@description:从对尾添加元素
@author yzh
@date 2019-04-20
@return
*/
@SuppressWarnings(“unchecked”)
@Override
public void addLast(E e) {
if (e == null){
throw new NullPointerException();
}
headLastListDueue.addByLast(e);
modCount++;
size++;
}
/***
@functionName:offerFirst
@description:从队列的开头实现元素的添加。入队成功返回true
@author yzh
@date 2019-04-20
@return
*/
@Override
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
/***
@functionName:offerLast
@description:从队列的末尾实现元素的添加。入队成功返回true
@author yzh
@date 2019-04-20
@return
*/
@Override
public boolean offerLast(E e) {
addLast(e);
return false;
}
/***
@functionName:removeFirst
@description:从队头实现元素的删除
@author yzh
@date 2019-04-20
@return
*/
@Override
public E removeFirst() {
//方法取出并删除队头的元素,当队列为空,返回null。
E e = pollFirst();
//如果元素为空,那么抛出空指针异常
if(e == null){
throw new NullPointerException();
}
return e;
}
/***
@functionName:removeLast
@description:从队尾实现元素的删除
@author yzh
@date 2019-04-20
@return
*/
@Override
public E removeLast() {
//方法取出并删除队尾的元素,当队列为空,返回null。
E e = pollLast();
//如果元素为空,那么抛出空指针异常
if(e == null){
throw new NullPointerException();
}
return e;
}
/***
@functionName:pollFirst
@description:取出并删除队头的元素,当队列为空,返回null
@author yzh
@date 2019-04-20
@return
*/
@SuppressWarnings(“unchecked”)
@Override
public E pollFirst() {
//如果队列为空,那么返回null
if(isEmpty()){
return null;
}
//获取对头元素
E e = (E) headLastListDueue.getHead();
//删除对头元素
headLastListDueue.deleteByHead();
modCount++;
size–;
return e;
}
/***
@functionName:pollLast
@description:取出并删除队尾的元素,当队列为空,返回null
@author yzh
@date 2019-04-20
@return
*/
@SuppressWarnings(“unchecked”)
@Override
public E pollLast() {
//如果队列为空,那么返回null
if(isEmpty()){
return null;
}
//获取尾元素
E e = (E) headLastListDueue.getLast();
//删除对尾元素
headLastListDueue.deleteByLast();
modCount++;
size–;
return e;
}
/***
@functionName:getFirst
@description:取出但不删除队头的元素,当队列为空,抛出异常
@author yzh
@date 2019-04-20
@return
*/
@SuppressWarnings(“unchecked”)
@Override
public E getFirst() {
//如果队列为空,那么抛出异常
if(isEmpty()){
throw new RuntimeException(“this dueue is null!”);
}
return (E) headLastListDueue.getHead();
}
/***
@functionName:getLast
@description:取出但不删除队尾的元素,当队列为空,抛出异常
@author yzh
@date 2019-04-20
@return
*/
@SuppressWarnings(“unchecked”)
@Override
public E getLast() {
//如果队列为空,那么抛出异常
if(isEmpty()){
throw new RuntimeException(“this dueue is null!”);
}
return (E) headLastListDueue.getLast();
}
/***
@functionName:peekFirst
@description:取出队头的元素,并不删除
@author yzh
@date 2019-04-20
@return
*/
@SuppressWarnings(“unchecked”)
@Override
public E peekFirst() {
return (E) headLastListDueue.getHead();
}
/***
@functionName:peekLast
@description:取出队尾的元素,并不删除
@author yzh
@date 2019-04-20
@return
*/
@SuppressWarnings(“unchecked”)
@Override
public E peekLast() {
return (E) headLastListDueue.getLast();
}
///Stack///
/***
@functionName:push
@description:入栈操作
@author yzh
@date 2019-04-20
@return
*/
@Override
public void push(E e) {
this.addLast(e);
}